LINQ和布尔值 [英] LINQ and Boolean Values

查看:92
本文介绍了LINQ和布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 LINQ 查询,该查询返回一个布尔值.

I have a LINQ Query which returns a Boolean.

     Boolean status=true;
     var s = from p in msg where p.Type == false select false;

我需要从LINQ查询的值中设置状态,这是布尔对此方法采取的方法

im required to set status from the value from the LINQ query which is a boolean how to take a approach to this

如果LINQ查询只有一个false,我需要返回一个false作为布尔值,这就是我需要的

If the LINQ query has a single false i need to return a false as Boolean thats what i need

推荐答案

目前假设p有很多msg,则s将成为可枚举的,且错误数目为零到零.您可能需要考虑使用其他运算符像

At the moment assumign p has lots of msgs then s is going to be an enumerable with zero to lots of falses in. You might want to consider a different operator like

!msg.Any(x=>x.Type=false)

或者更好地感谢Ray的评论:

Or better thanks to Ray in comments:

msg.All(x => x.Type)

如果p中的任何条件满足条件,则Anyny返回true.如果所有条件都满足,则All返回true(因此,如果发现不匹配的条件,则将立即返回).

Any returns true if the condition is satisfied by anything in p. All returns true if the condition is satisfied in all of them (and will thus immediately return if it finds one that doesn't match).

或者用您的代码也可以:

Alternatively with your code you could just do:

status = s.Count()==0;

如果您的s为空(其中没有错误),则它将返回true;如果具有一个或多个false,则它将返回false.

If your s is empty (no falses in it) then it will return true, if it has one or more falses it will return false.

这篇关于LINQ和布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆