C#之Linq、where()、FindAll()的区别
C#之Linq、where()、FindAll()的区别
对于实现了IEnumerable接口的类、类型、集合可以使用Linq、Linq的扩展方法where()、FindAll()来查询符合谓词约束的数据。这三者之间执行的方式是不一样的,同样的数据同样的查询条件返回的结果也不相同。先上代码再分析。
public static void LaterSelect()
{
var names = new List { "Nino", "Alberto", "Juan", "Mike", "Phil"
};
var namesWithJ = from n in names
where n.StartsWith("J")
orderby n
select n;
var namesWithJforWhere = names.Where(n => n.StartsWith("J"));
var namesWithJforFindAll = names.FindAll(n => n.StartsWith("J"));
Console.WriteLine("First iteration by Linq:");
foreach (string name in namesWithJ)
{
Console.WriteLine(name);
}
Console.WriteLine("First iteration by names.Where():");
foreach (var name in namesWithJforWhere)
{
Console.WriteLine(name);
}
Console.WriteLine("First iteration by names.FindAll():");
foreach (var name in namesWithJforFindAll)
{
Console.WriteLine(name);
}
Console.WriteLine();
names.Add("John");
names.Add("Jim");
names.Add("Jack");
names.Add("Denny");
Console.WriteLine("Second iteration by Linq:");
foreach (string name in namesWithJ)
{
Console.WriteLine(name);
}
Console.WriteLine("Second iteration by names.Where():");
foreach (var name in namesWithJforWhere)
{
Console.WriteLine(name);
}
Console.WriteLine("Second iteration by names.FindAll():");
foreach (var name in namesWithJforFindAll)
{
Console.WriteLine(name);
}
}
输出结果:
分析
从输出结果可以看出,Linq和where两种方式的结果是相同的,说明这两者之间没有区别,当定义一个查询后并没有立即执行查询而是在执行foreach循环的时候才执行查询而且是在每一次执行foreach循环的时候去执行查询,这样只要数据是变化的结果就会是变化的。但是在一些情况下,这是不可行的。调用扩展方法 ToArray()、ToEnumerable()、ToList()等可以改变这个操作,使返回的结果始终一致,这里是在调用To*这些方法时执行了一次查询而且不是延迟查询。FindAll()方法和Linq、where是有区别的,FindAll()不是延迟加载或者说延迟查询,当定义一个查询后就立即查询出所有符合条件的数据保存在变量中。
C#之Linq、where()、FindAll()的区别的更多相关文章
- 缓冲区 粘包 029 send 和sendall 的区别 find 和 findall 的区别
一.tcp : 属于长连接 与客户端连接了之后 其他客户端需要等待 要连接另外一个 必须优雅的断开前面这个客户的连接. 二.缓冲区 :为了避免网络传输信号不通畅而是程序一直停留在消息发送状态而不向下进 ...
- (转)selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
selenium-webdriver中获取页面元素的方式有很多,使用注解获取页面元素是其中一种途径, 方式有3种:@FindBy.@FindBys.@FindAll.下文对3中类型的区别和使用场景进行 ...
- @FindBy、@FindBys、@FindAll的区别
原文地址http://blog.csdn.net/tea_wu/article/details/21080789 selenium-webdriver中获取页面元素的方式有很多,使用注解获取页面元素是 ...
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
selenium-webdriver中获取页面元素的方式有很多,使用注解获取页面元素是其中一种途径, 方式有3种:@FindBy.@FindBys.@FindAll.下文对3中类型的区别和使用场景进行 ...
- LINQ to Entities 和LINQ to Objects 的区别
本文资料来源:http://www.codeproject.com/Articles/246861/LINQ-to-Entities-Basic-Concepts-and-Features) LINQ ...
- Python:正则表达式(一):search()、match()、findall() 的区别
以前一直觉得正则很难,不会用,今天试验了几个方法,整理总结了一下,简洁明了. 简单来讲,正则就是 提取一段特征数据,用(.*?)代替. 自己总结的特点&区别: (.*) 贪婪匹配,会尽可能的往 ...
- 第二节: 比较EF的Lambda查询和Linq查询写法的区别
简介 在前面EF的介绍中,曾多次提到过EF可以使用Lambda和Linq来完成对数据库的访问,这两种的语法的具体使用和注意事项在前面的DotNet进阶的系列章节中已经详细介绍过了,本次借着EF章节,重 ...
- linq to sql 和linq to php 的区别
linq to sql 这是自.net框架3.5版本以上做出了相关规定. linq to php .Net的linq库的忠实移植到PHP 这个库使得大量使用匿名函数在PHP 5.3中引入的功能.因此, ...
- python爬虫笔记之re.match匹配,与search、findall区别
为什么re.match匹配不到?re.match匹配规则怎样?(捕一下seo) re.match(pattern, string[, flags]) pattern为匹配规则,即输入正则表达式. st ...
- Linq的查询操作符
Linq有表达式语法和调用方法的语法.两者是可以结合使用,通常情况下也都是结合使用.表达式语法看上去比较清晰而调用方法的语法实现的功能更多,在此文章中介绍的是表达式语法.方法语法可以看System.L ...
随机推荐
- CSS笔记 - fgm练习 2-8 - 简易日历
<style> *{margin: 0; padding: 0} .outer{ width: 240px; margin: 10px auto; background: #f0f0f0; ...
- windows下安装emscripten
windows下安装emscripten windows下安装emscripten需要python.git环境 python安装 git安装 开始安装 # 1.克隆emsdk git clone ht ...
- sshfs 通过ssh 挂载远程目录
安装:yum -y install sshfs 挂载远程 ssh 文件系统: sshfs -p 1234 root@192.168.1.218:/home/ /mnt/ sshfs -p SSH端口 ...
- JavaScript字符串替换replace方法
在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...
- [React Intl] Use Webpack to Conditionally Include an Intl Polyfill for Older Browsers
Some browsers, such as Safari < 10 & IE < 11, do not support the JavaScript Internationali ...
- [React Intl] Format a Date Relative to the Current Date Using react-intl FormattedRelative
Given a date, we’ll use the react-intl FormattedRelative component to render a date in a human reada ...
- UVA 11489 - Integer Game 博弈
看题传送门 题目大意: S和T在玩游戏,S先.给出一数字串,两人轮流取出一个数字,要求每次取完之后剩下的数为3的倍数,或者没有数字留下.如果两个人足够聪明,求胜利的一方. 思路: 我一开始竟然没有输C ...
- HIVE快速入门 分类: B4_HIVE 2015-06-06 11:27 59人阅读 评论(0) 收藏
(一)简单入门 1.创建一个表 create table if not exists ljh_emp( name string, salary float, gender string) commen ...
- 关于Linux启动时挂载rootfs的几种方式
一直对Linux启动时挂载根文件系统的过程存在着很多疑问,今天在水木精华区找到了有用的资料,摘录如下: 1.Linux启动时,经过一系列初始化之后,需要mount 根文件系统,为最后运行init进程等 ...
- 使用Perl脚本编译Latex
使用Perl脚本编译Latex 脚本能实现Latex文本的初级编译,并将生成的中间文件移动到同一个目录 调用方法 chmod +x xelatex2pdf.pl xelatex2pdf.pl -n 2 ...