操作符 | 说明 |
---|---|
聚合 | |
Aggregate | 对序列执行一个自定义方法 |
Average | 计算数值序列的平均值 |
Count | 返回序列中的项目数(整数) |
LongCount | 返回序列中的项目数(长型) |
Min | 查找数字序列中的最小数 |
Max | 查找数字序列中的最大数 |
Sum | 汇总序列中的数字 |
连接 | |
Concat | 将两个序列连成一个序列 |
转换 | |
Cast | 将序列中的元素转换成指定类型 |
OfType | 筛选序列中指定类型的元素 |
ToArray | 从序列返回一个数组 |
ToDictionary | 从序列返回一个字典 |
ToList | 从序列返回一个列表 |
ToLookup | 从序列返回一个查询 |
ToSequence | 返回一个 IEnumerable 序列 |
元素 | |
DefaultIfEmpty | 为空序列创建默认元素 |
ElementAt | 返回序列中指定索引的元素 |
ElementAtOrDefault | 返回序列中指定索引的元素,或者如果索引超出范围,则返回默认值 |
First | 返回序列中的第一个元素 |
FirstOrDefault | 返回序列中的第一个元素,或者如果未找到元素,则返回默认值 |
Last | 返回序列中的最后一个元素 |
LastOrDefault | 返回序列中的最后一个元素,或者如果未找到元素,则返回默认值 |
Single | 返回序列中的单个元素 |
SingleOrDefault | 返回序列中的单个元素,或者如果未找到元素,则返回默认值 |
相等 | |
SequenceEqual | 比较两个序列看其是否相等 |
生成 | |
Empty | 生成一个空序列 |
Range | 生成一个指定范围的序列 |
Repeat | 通过将某个项目重复指定次数来生成一个序列 |
分组 | |
GroupBy | 按指定分组方法对序列中的项目进行分组 |
联接 | |
GroupJoin | 通过归组将两个序列联接在一起 |
Join | 将两个序列从内部联接起来 |
排序 | |
OrderBy | 以升序按值排列序列 |
OrderByDescending | 以降序按值排列序列 |
ThenBy | 升序排列已排序的序列 |
ThenByDescending | 降序排列已排序的序列 |
Reverse | 颠倒序列中项目的顺序 |
分区 | |
Skip | 返回跳过指定数目项目的序列 |
SkipWhile | 返回跳过不满足表达式项目的序列 |
Take | 返回具有指定数目项目的序列 |
TakeWhile | 返回具有满足表达式项目的序列 |
投影 | |
Select | 创建部分序列的投影 |
SelectMany | 创建部分序列的一对多投影 |
限定符 | |
All | 确定序列中的所有项目是否满足某个条件 |
Any | 确定序列中是否有任何项目满足条件 |
Contains | 确定序列是否包含指定项目 |
限制 | |
Where | 筛选序列中的项目 |
设置 | |
Distinct | 返回无重复项目的序列 |
Except | 返回代表两个序列差集的序列 |
Intersect | 返回代表两个序列交集的序列 |
Union | 返回代表两个序列交集的序列 |
int[] nums = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 }; int singleNum = nums.Single(x => x > 16 && x < 64); Console.WriteLine(singleNum.ToString());
int singleNum = nums.Single<int>( delegate(int x) {return (x > 16 && x < 64); } ) ;
int[] nums = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 }; int num1 = nums.First<int>(); int num2 = nums.First<int>(x => x > 50); int num3 = nums.FirstOrDefault<int>(x => x > 5000); Console.WriteLine( num1.ToString() + "-" + num2.ToString() + "-" + num3.ToString());
using (Entities entities = new Entities()) { var query = (from c in entities.Customers select c).First(c => c.City.Equals("London")); Console.WriteLine(query.CompanyName); }
using (Entities entities = new Entities()) { var query = (from c in entities.Customers where c.CustomerID.Equals("BOLID") select c).First(); Console.WriteLine(query.CompanyName); }
using (Entities entities = new Entities()) { var query = from o in entities.Orders where o.OrderDetails.Sum( od => od.UnitPrice * od.Quantity) >= 10000 select o; foreach (Orders order in query) Console.WriteLine(order.OrderID); }
using (Entities entities = new Entities()) { var query = from c in entities.Customers where c.Orders.Sum( o => o.OrderDetails.Sum( od => od.UnitPrice * od.Quantity)) >= 25000 select c; foreach (Customers customer in query) Console.WriteLine(customer.CompanyName); }
using (Entities entities = new Entities()) { var query = (from c in entities.Customers where c.Orders.Sum( o => o.OrderDetails.Sum( od => od.UnitPrice * od.Quantity)) >= 25000 select c).Count(); Console.WriteLine(query); }
using (Entities entities = new Entities()) { var query = (from c in entities.Customers select new { c.CustomerID, Total = c.Orders.Sum( o => o.OrderDetails.Sum(od => od.UnitPrice)) }).Max(c2 => c2.Total); Console.WriteLine(query); }
using (Entities entities = new Entities()) { var query = from c in entities.Customers where c.Orders.Sum( o => o.OrderDetails.Sum(od => od.UnitPrice)) > 0 select new { c.CustomerID, Total = c.Orders.Sum( o => o.OrderDetails.Sum(od => od.UnitPrice)) }; foreach (var item in query.OrderByDescending(x => x.Total)) Console.WriteLine(item.CustomerID + " == " + item.Total); }
foreach (var item in query.OrderByDescending(x => x.Total) .ThenBy(x => x.CustomerID)) { Console.WriteLine(item.CustomerID + " == " + item.Total); }
using (Entities entities = new Entities()) { bool allUKCustomerAreFromLondon = (from c in entities.Customers where c.Country == "UK" select c).All( c => c.City.Equals("London")); Console.WriteLine(allUKCustomerAreFromLondon ? "Yes" : "No"); }
using (Entities entities = new Entities()) { bool isOneUKCustomerFromCowes = (from c in entities.Customers where c.Country == "UK" select c).Any( c => c.City.Equals("Cowes")); Console.WriteLine(isOneUKCustomerFromCowes? "Yes" : "No"); }
using (Entities entities = new Entities()) { Customers customerBSBEV = (from c in entities.Customers where c.CustomerID == "BSBEV" select c).First(); var customersUK = from c in entities.Customers where c.Country == "UK" select c; bool isCustomerInSequence = customersUK.Contains(customerBSBEV); Console.WriteLine(isCustomerInSequence? "Yes" : "No"); }
using (Entities entities = new Entities()) { ... bool isCustomerInSequence = customersUK.Contains(customerBSBEV, new CustomerComparer()); Console.WriteLine(isCustomerInSequence? "Yes" : "No"); }
private class CustomerComparer : IEqualityComparer<Customers> { public bool Equals(Customers x, Customers y) { if (x == null || y == null) return false; return x.CustomerID.Equals(y.CustomerID); } ... }
来源: http://msdn.microsoft.com/zh-cn/magazine/cc337893.aspx