LINQ系列:Linq to Object集合操作符
集合操作符对元素的集合或序列集合进行操作,并返回一个集合。LINQ共有4种集合查询操作符:Distinct、Union、Intersect和Except。
1. Distinct
Distinct操作符删除集合中重复的值,并返回该集合中互不相同的元素。
1>. 原型定义
- public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source);
- public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer);
2>. 示例
- int[] fibonacci = new int[] { , , , , , , , };
- var expr = from f in fibonacci
- select f;
- expr.Distinct();
- int[] fibonacci = new int[] { , , , , , , , };
- var expr = from f in fibonacci
- where f >
- select f;
- expr.Distinct();
- var expr = from p in context.Products
- select p.ProductName;
- expr.Distinct();
- var expr = context.Products
- .Select(p => p.ProductName)
- .Distinct();
- int[] fibonacci = new int[] { , , , , , , , };
- var expr = fibonacci.Distinct().Count();
- var expr = context.Products
- .Select(c => c.CategoryID)
- .Distinct()
- .Count();
- int[] fibonacci = new int[] { , , , , , , , };
- var expr = fibonacci.Count(f => f % == );
2. Union
Union操作符返回两个序列或集合并集中的每个互不相同的元素。与Concat操作符不同,Union操作符返回互不相同的元素,而Concat操作符将返回所有的值。
1>. 原型定义
- public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
- public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);
2>. 示例
- int[] fibonacci = new int[] { , , , , , , , };
- int[] factorial = new int[] { , , , , };
- IEnumerable<int> expr = fibonacci.Union(factorial);
- foreach (int item in expr)
- {
- Console.Write(item + " ");
- }
3. Intersect
Intersect操作符返回两个序列的交集,即返回那些同时存在于两个序列或集合中的值。
1>. 原型定义
- public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
- public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);
2>. 示例
- int[] fibonacci = new int[] { , , , , , , , };
- int[] factorial = new int[] { , , , , };
- IEnumerable<int> expr = fibonacci.Intersect(factorial);
- foreach (int item in expr)
- {
- Console.Write(item + " ");
- }
4. Except
Except操作符与Intersect操作符相反,它返回两个序列中不同的部分,返回序列中所有不重复的值。
1>. 原型定义
- public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
- public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);
2>. 示例
- int[] fibonacci = new int[] { , , , , , , , };
- int[] factorial = new int[] { , , , , };
- IEnumerable<int> expr = fibonacci.Except(factorial);
- foreach (int item in expr)
- {
- Console.Write(item + " ");
- }
LINQ系列:Linq to Object集合操作符的更多相关文章
- LINQ系列目录
1. LINQ准备 1.1 C#中与LINQ相关特性 2. LINQ to Object 2.1 LINQ to Object投影操作符(Select/SelectMany/Let) 2.2 LINQ ...
- C# ~ 从 XML 到 Linq 到 Linq to XML
.XML 可扩展标记语言 (Extensible Markup Language), 标记 (markup) 是关键部分,是标准通用标记语言 (Standard Generalized Markup ...
- LinQ系列文章
温故而知新,想着系统再学习一次LinQ知识点,发现园子里有个非常棒的系列文章,所以Mark下来,方便以后查阅! 系列博客导航: LINQ之路系列博客导航 LINQ之路 1:LINQ介绍 LINQ之路 ...
- Linq/List/Array/IEnumerable等集合操作
来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1 LINQ查询结果集 1 2 Sy ...
- LINQ 系列
C#图解教程 第十九章 LINQ LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...
- C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换
LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...
- Linq 支持动态字查询集合, 也就是说根据传入的值进行查询。
Linq 支持动态字查询集合, 也就是说根据传入的值进行查询. 比如我们有个类Patient, 其中有个字段PatientName, 现在有Patient集合, 想要查询PatientName为&qu ...
- Linq系列
LINQ 图解 Linq中的Select——投影 Linq学习资源 Expert C# 5.0中的Linq部分
- LINQ之LINQ to Objects(上)
LINQ概述 LINQ,语言集成查询(Language Integrated Query),它允许使用C#或VB代码以查询数据库相同的方式来操作不同的数据源. 1.LINQ体系结构 从上图可以看出,L ...
随机推荐
- Ajax工作流程
Ajax通过XMLHttpRequest对象实现异步方式在后台发送发送请求. 主要有以下四个步骤: (1)初始化XMLHttpRequest对象.不同浏览器的差异,需要我们创建一个跨浏览器的对象,并判 ...
- xpath使用
一.安装(win7 64) 1.安装lxml,pip install lxml 2.如果安装出错,下载lxml-3.5.0b1.win-amd64-py2.7.exe: 地址:http://www.l ...
- 关于ARC下需要dealloc的相关内容
今天在项目中使用KVO添加观察者模式的时候,在返回上一级的时候竟然崩了.可是,看了很久,代码没有问题.最后,终于知道了需要添加dealloc 防止以后再出错,所以,便纪录下来.关于ARC下需要手动释放 ...
- getComputedStyle的应用
后面有例子,所以把HTML,CSS样式写在前面 HTML结构: <div id="myDiv" style="background-color: lightseag ...
- AJax登录。。转
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX 是一种用于创建快速 ...
- javascript 原型及原型链的初步理解
最近折腾了好久,终于是把js里面的原型和原型链做了个初步的理解: 在这里,我打个比喻: 我(child),我妈constructor(构造函数)生了我:别人问我老妈跟谁生的我,于是此时我妈会指向我爸爸 ...
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- 纯css3 Star
<style><!--* { box-sizing: border-box; padding: 0px; margin: 0px; } body, html { height: 10 ...
- 学习建模 - UML
最轻量级的工具下载地址 http://staruml.io/download 下载解压依赖:libgcrypt11 https://pan.baidu.com/s/1i3wb6M5 学习地址 http ...
- 转:隐马尔可夫模型(HMM)攻略
隐马尔可夫模型 (Hidden Markov Model,HMM) 最初由 L. E. Baum 和其它一些学者发表在一系列的统计学论文中,随后在语言识别,自然语言处理以及生物信息等领域体现了很大的价 ...