开发者

LINQ query where boolean value is true or false

开发者 https://www.devze.com 2023-04-13 04:47 出处:网络
How can I compare a boolean value to both true and false in a LINQ query? If hideCompleted is true, I want to show values where IsC开发者_如何学Goompleted is false

How can I compare a boolean value to both true and false in a LINQ query?

If hideCompleted is true, I want to show values where IsC开发者_如何学Goompleted is false If hideCompleted is false, I want to show values where IsCompleted is true or false

Example:

(t1.IsCompleted ?? false == (hideCompleted == true ? false : (true || false)))


Just to be sure I understand you correctly, if hideCompleted is false, you don't care what the value of IsCompleted is? If so...

!(hideCompleted && t1.IsCompleted)


Build your query based on hideCompleted being true, similar to this approach:

var query = dc.SomeTable;
if (hideCompleted)
{
    query = query.Where(t1 => !t1.IsCompleted);
}

This way when hideCompleted is true you filter for t1.IsCompleted being false. When hideCompleted is false your original query will grab all results regardless of the value of t1.IsCompleted.


you can use this condition

where (hideCompleted==true && t1.IsCompleted==false) || (hideCompleted==false)
0

精彩评论

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

关注公众号