开发者

switch case in where clause of linq

开发者 https://www.devze.com 2023-02-16 20:08 出处:网络
how i can us开发者_如何学Ce switch case in where clause as well as select statement can anyone give me examples...You can not use switch, you need a construct that returns a value (switch does not ret

how i can us开发者_如何学Ce switch case in where clause as well as select statement can anyone give me examples...


You can not use switch, you need a construct that returns a value (switch does not return a value in c#), like the ternary operator <cond> ? <trueValue> : <falseValue>.

You can nest them, it will be a bit messy, but should work.

cond1 ? valueFor1 :
       (cond2 ? valueFor2 : 
               (cond3 ? valueFor3 : 
                        defaultValue))

but in the where-clause it is usually simpler to combine your conditions with && and ||.

0

精彩评论

暂无评论...
验证码 换一张
取 消