简体   繁体   English

基于linq查询中的另一个值设置一个值

[英]setting a value based on another value in a linq query

I'm changing a variable from a string a byte in my business logic but I'm not touching the database field that contains the value yet. 我在业务逻辑中将一个字符串中的变量从一个字节更改为一个字节,但是我尚未触及包含该值的数据库字段。 So in the meantime, I'm looking for ac# equivalent to javascript's conditional syntax SomeValue = (TestValue === Something) ? 1 : 2; 因此,与此同时,我正在寻找与javascript的条件语法SomeValue = (TestValue === Something) ? 1 : 2;等效的ac# SomeValue = (TestValue === Something) ? 1 : 2; SomeValue = (TestValue === Something) ? 1 : 2;

Basically, the linq-to-sql query I have looks like this: 基本上,我的linq-to-sql查询看起来像这样:

var TheOutput = from x in MyDC.SomeTable
                ....
                select new SomeModel()
                {
                     SomeByte = (x.SomeField === "test") ? 1 : 0
                }

SomeField is a string and if it's equal to some test string then I want the property of the output's model to be set to a byte. SomeField是一个字符串,如果它等于某个测试字符串,那么我希望将输出模型的属性设置为一个字节。

采用

(x.SomeField == "test") ? 1 : 0;

Cast to byte explicitly. 显式转换为字节。 SomeByte = (byte)(x.SomeField=="test"?1:0) should work. SomeByte = (byte)(x.SomeField=="test"?1:0)应该起作用。

for example 例如

 select new SomeModel()
            {
                 SomeByte = (x.SomeField == "test") ? Convert.ToByte(1); : Convert.ToByte(0);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM