当前位置:网站首页>C# Any()和AII()方法
C# Any()和AII()方法
2022-06-27 09:35:00 【dotNET跨平台】

我们常常需要的另一类查询是确定数据是否满足某个条件,或者确保所有数据都满足某个条件。例如,需要确定某个产品是否已经脱销(库存为 0),或者是否发生了某个交易。

LINQ 提供了两个布尔方法:Any()和 All(),它们可以快速确定对于数据而言,某个条件是 true 还是 false。因此很容易地找到数据,如下面的示例所示。

按照下面的步骤在Visual Studio 中创建示例:
(1)创建一个新的控制台应用程序。

(2)创建 Customer 类的代码和初始化顾客列表(List<Customer>customers)的代码。

(3)在Main() 方法中,在 customers 列表初始化和查询声明后,删除处理循环,输入如下所示的代码:
bool anyUSA = customers.Any(c => c.Country == "USA");
if (anyUSA)
{
Console.WriteLine("Some customers are in the USA");
}
else
{
Console.WriteLine("No customers are in the USA");
}
bool allAsia = customers.All(c => c.Region == "Asia");
if (allAsia)
{
Console.WriteLine("All customers are in Asia");
}
else
{
Console.WriteLine("Not all customers are in Asia");
}
(4)编译并执行程序,将看到一些消息,指出一些顾客来自美国,并不是所有的顾客都来自亚洲:
Some customers are in the USA
Not all customers are in Asia
Program finished, press Enter/Return to continue:
示例的说明
Customer 类和 customers 列表的初始化与前面例子中的相同。在第一个查询语句中,调用了 Any() 方法,用一个简单的 Lambda 表达式检查 Customer Country 字段的值是不是USA:
bool anyUSA = customers.Any(c => c.Country == "USA");
LINQ方法Any() 把传送给它的 Lambda 表达式 c=>c.Country=="USA"应用于customers 列表中的所有数据,如果对于列表中的任意顾客,Lambda 表达式是 true,就返回 true。

接着检查 Any() 方法返回的布尔结果变量,输出一个消息,显示查询的结果 Any()方法虽然仅返回 true 或 false,但它会执行一个查询,得到 true 或 false 结果):
if (anyUSA)
{
Console.WriteLine("Some customers are in the USA");
}
else
{
Console.WriteLine("No customers are in the USA");
}
虽然可以通过一些巧妙的代码使这个消息更紧凑一些,但这里的代码比较直观,便于理解。anyUSA 设为 true, 因为数据集中的确有顾客居住在美国,所以看到了消息 Some customers are in the USA.

在下一个查询语句中,调用了 AII() 方法,利用另一个简单的 Lambda 表达式确定是否所有的顾客都来自亚洲:
bool allAsia = customers.All(c=> c.Region =="Asia");
LINQ 方法All() 把 Lambda 表达式应用于数据集,并返回 false,因为有一些顾客不是来自亚洲。然后根据 allAsia 的值返回相应的消息。


微信公众号
DotNet讲堂
边栏推荐
- ucore lab5
- 使用aspose-slides将ppt转pdf
- Digital ic-1.9 understands the coding routine of state machine in communication protocol
- 运维一线工作常用shell脚本再整理
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- Scientists develop two new methods to provide stronger security protection for intelligent devices
- 三道基础面试题总结
- Use CAS to complete concurrent operations with atomic variables
- 1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)
- 枚举?构造器?面试Demo
猜你喜欢

【系统设计】邻近服务

【生动理解】深度学习中常用的各项评价指标含义TP、FP、TN、FN、IoU、Accuracy

Digital ic-1.9 understands the coding routine of state machine in communication protocol

SVN版本控制器的安装及使用方法

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!

高等数学第七章微分方程

Quick start CherryPy (1)

How do I get the STW (pause) time of a GC (garbage collector)?

NoSQL database redis installation
随机推荐
视频文件太大?使用FFmpeg来无损压缩它
Tips for using Jupiter notebook
Curiosity mechanism in reinforcement learning
高等数学第七章微分方程
Pakistani security forces killed 7 terrorists in anti-terrorism operation
ThreadLocal digs its knowledge points again
Source insight 工具安装及使用方法
巴基斯坦安全部队开展反恐行动 打死7名恐怖分子
The markdown plug-in of the browser cannot display the picture
prometheus告警流程及相关时间参数说明
1098 insertion or heap sort (PAT class a)
Decompile the jar package and recompile it into a jar package after modification
一次线上移动端报表网络连接失败问题定位与解决
js中的数组对象
Several cases that do not initialize classes
Conception de plusieurs classes
【OpenCV 例程200篇】212. 绘制倾斜的矩形
Principle and application of the most complete H-bridge motor drive module L298N
内存泄露的最直接表现
提高效率 Or 增加成本,开发人员应如何理解结对编程?