http://www.cnblogs.com/wyqlijin/archive/2011/02/25/1964934.html 这位仁兄写的比较高深,建议大家看看
一:
这一篇以一个数据类为例,操作数据。
先建立一个数据类型custom
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinqTest { class custom { private string _id; private string _city; private string _country; private string _region; private int _sales; public string ID { get { return _id; } set { _id = value; } } public string City { get { return _city; } set { _city = value; } } public string Country { get { return _country; } set { _country = value; } } public string Region { get { return _region; } set { _region = value; } } public int Sales { get { return _sales; } set { _sales = value; } } public override string ToString() { return "ID:" + ID + "City:" + City + "Country:" + Country + "Region:" + Region + "Sales:" + Sales; } } }
在另外一个class中用函数创建一个List<cutome>数据集合
public List<custom> GetDataList() { List<custom> customers = new List<custom> { new custom { ID="A", City="New York", Country="USA", Region="North America", Sales=9999}, #region all data new custom { ID="B", City="Mumbai", Country="India", Region="Asia", Sales=8888}, new custom { ID="C", City="Karachi", Country="Pakistan", Region="Asia", Sales=7777}, new custom { ID="D", City="Delhi", Country="India", Region="Asia", Sales=6666}, new custom { ID="E", City="São Paulo", Country="Brazil", Region="South America", Sales=5555 }, new custom { ID="F", City="Moscow", Country="Russia", Region="Europe", Sales=4444 }, new custom { ID="G", City="Seoul", Country="Korea", Region="Asia", Sales=3333 }, new custom { ID="H", City="Istanbul", Country="Turkey", Region="Asia", Sales=2222 }, new custom { ID="I", City="Shanghai", Country="China", Region="Asia", Sales=1111 }, new custom { ID="J", City="Lagos", Country="Nigeria", Region="Africa", Sales=1000 }, new custom { ID="K", City="Mexico City", Country="Mexico", Region="North America", Sales=2000 }, new custom { ID="L", City="Jakarta", Country="Indonesia", Region="Asia", Sales=3000 }, new custom { ID="M", City="Tokyo", Country="Japan", Region="Asia", Sales=4000 }, new custom { ID="N", City="Los Angeles", Country="USA", Region="North America", Sales=5000 }, new custom { ID="O", City="Cairo", Country="Egypt", Region="Africa", Sales=6000 }, new custom { ID="P", City="Tehran", Country="Iran", Region="Asia", Sales=7000 }, new custom { ID="Q", City="London", Country="UK", Region="Europe", Sales=8000 }, new custom { ID="R", City="Beijing", Country="China", Region="Asia", Sales=9000 }, new custom { ID="S", City="Bogotá", Country="Colombia", Region="South America", Sales=1001 }, new custom { ID="T", City="Lima", Country="Peru", Region="South America", Sales=2002 } #endregion }; return customers; }
下面写两个方法,分别调用这个数据集合
方法一:选择出所有Region == "Asia" 的数据
public void Test() { List<custom> customers = GetDataList(); //get data list var result = from item in customers where item.Region == "Asia" select item; Console.WriteLine("-------------------customer in Asia:------------"); foreach (var item in result) { Console.WriteLine(item); //Console.WriteLine(item.ToString()); has same affect //Console.WriteLine("Id:{0},city:{1},country:{2},region:{3},sales:{4}", item.ID, item.City, item.Country, item.Region, item.Sales); } }
方法二:利用linq的投影,也就是 select new { item.ID, item.Region, item.Sales }; 利用匿名函数的方法选择出item中的三个字段
在方法一种,将select item 改为select item.id 其实也是一种投影!就像sql中所说的一样
值得注意的是:在方法中,select子句的参数类表只能是一个字段,所以形如 select item.id ,item.region,item.sales 是错误的
这里在select子句中直接用C#匿名类型创建语法,创建一个未命名的对象类型,只带有三个属性,select创建了新的对象,这样只会复制3个属性,完成处理查询的不同阶段
Console.WriteLine(item);这个通用代码,编译器会推断出查询结果的类型,给匿名类型调用正确的方法,无需我们显式编写代码,甚至不用重写tostring()方法。因为编辑器提供了tostring()方法的默认实现代码,以类似于对象初始化的方式输出属性和名称
public void TestProjection() { List<custom> customers = GetDataList(); var result = from item in customers where item.Region == "North America" select new { item.ID, item.Region, item.Sales }; //匿名类
Console.WriteLine("-------------------Projection function test------------"); foreach (var item in result) { Console.WriteLine(item); } }
我们还可以用Lamabda表达式写上面的方法,
注意在下面的customer.where().select()中的where和select位置是不固定的,因为编译器从头往后编译,先编译
select()再编译where()。也就是在结果集中先select出字段,再用where给select出来的字段做限制
//采用linq方法语法(Lamabda表达式)的查询语句 public void TestLamadbaProjection() { List<custom> customers = GetDataList(); //select(n=>new{n.region,n.id}) select 创建匿名类型 var result = customers.Select(n => new { n.Region,n.Sales,n.ID}.Where(n => n.Region == "North America");
foreach (var item in result) { Console.WriteLine(item); //c#会推断类型,编辑器会根据推断出来的类型,以类似于对象初始化的方式输出属性和名称 } }
结果:
二:Linq中常用的其他方法
linq中常用的方法还有:
1.distinct()选择出唯一值,类似于sql中的distinct
example:var result=dataList.select (n=>n.id).Dictinct(); ----Lambda
var result=( from item in datalist select item.id).Distinct(); -----Linq
2. All(),Any () 数据集datalist中所有数据,或者至少一个数据符合条件,注意返回的是bool类型,表示在datalist中是否存在符合条件的数据
example:
List<custom> customers = GetDataList(); var result = customers.Any(n=>n.Region=="North America"); var result1 = customers.All(n=>n.Region=="North America"); var result2 = (from item in customers select item).All(n=>n.Region=="North America");
3.多级排序 order by
example:
var result = from item in customers orderby item.Region, item.ID descending, item.City select item;
使用Lambda实现的多级排序:
var result4 = customers.OrderBy(n => n.ID). ThenBy(n => n.City). ThenByDescending(n => n.Region). Select(n => new {n.ID,n.Region,n.City });
4.分组 Group by
public void GroupByTest() { Console.WriteLine("-----------------Group by Test----------------------"); List<custom> customers = GetDataList(); //获取所有的数据 var result = from item in customers group item by item.Region into rg //按照item的item.region进行分组,结果放到rg中 select new { totalSales = rg.Sum(item => item.Sales), Region = rg.Key }; //select new创建匿名类型,自定义匿名类型中包含的属性 //rg是分组结果,每个分组中的基本单元还是item,所以rg.sum是对每个分组中的item进行sum操作。 //re.key 其中的key是分组主键 foreach (var item in result) { Console.WriteLine(string.Format("Region:{0,-20} Sales:{1,-20}",item.Region,item.totalSales)); //foreach循环,并且左对齐输出
} }
分组查询中的数据通过一个键(key)字段来分组,每个组中的所有成员都共享这个字段值。在这个例子中,键字段是Region:
group c by c.Region
要计算每个组的总和,应生成一个新的结果集cg:
group c by c.Region into cg
在select子句中,投影了一个新的匿名类型,其属性是总销售量(通过引用cg结果集来计算)和组的键值,后者是用特殊的组Key来引用的:
select new { TotalSales = cg.Sum(c => c.Sales), Region = cg.Key }
组的结果集实现了LINQ接口IGrouping,它支持Key属性。我们总是要以某种方式引用Key属性,来处理组合的结果,因为该属性表示创建数据中的每个组时使用的条件。
5.Take(),Skip()选择结果集中前几项,或者跳过多少项
example:
var take=datalist.Take(5); -----选择前5项
var skipThenTake=datalist.Skip(5).Take(5); ----跳过前5项选择后面的5项
6.First(),返回符合条件的第一行FirstOrDefault()来处理没有的情况(any,all只是返回true,false而已,而改方法返回是是确切是数据)
example:
Console.WriteLine("A customer in Africa");
Console.WriteLine(queryResults.First(c => c.Region == "Africa"));
Console.WriteLine("A customer in Antarctica");
Console.WriteLine(queryResults.FirstOrDefault(c => c.Region == "Antarctica"));
结果:A customer in Africa
{ City = Lagos, Country = Nigeria, Region = Africa }
A customer in Antarctica(还有一空行)
7.union,except,Intersect 并集,差集,交集
public void UnionExceptIntersec() { List<custom> customers = GetDataList(); var quertyTop5 = customers.Take(5); var quertyKip3Top5 = customers.Skip(3).Take(5); Console.WriteLine("-----------------top 5---------------------"); foreach (var item in quertyTop5) { Console.Write(item.ID+"-"); } Console.WriteLine(); Console.WriteLine("--------------- skip 3 --top 5---------------------"); foreach (var item in quertyKip3Top5) { Console.Write(item.ID+"-"); } Console.WriteLine(); Console.WriteLine("--------------- Union(并集)---------------------"); var union = quertyTop5.Union(quertyKip3Top5); foreach (var item in union) { Console.Write(item.ID + "-"); } Console.WriteLine(); Console.WriteLine("---------------Intersect(交集) ---------------------"); var Intersect = quertyTop5.Intersect(quertyKip3Top5); foreach (var item in Intersect) { Console.Write(item.ID + "-"); } Console.WriteLine(); Console.WriteLine("---------------Except(差集) ---------------------"); var Except = quertyTop5.Except(quertyKip3Top5); foreach (var item in Intersect) { Console.Write(item.ID + "-"); } }
8.join 左右拼接
public void Join() { Console.WriteLine("-------------------------Join-----------------"); List<custom> customers = GetDataList(); var quertyTop5 = customers.Take(5); var quertyKip3Top5 = customers.Skip(3).Take(5); var result= from item1 in quertyTop5 join item2 in quertyKip3Top5 on item1.ID equals item2.ID select new { item1.ID,item1.Region, count=item1.Sales+item2.Sales}; foreach (var item in result) { Console.WriteLine(item); } }
以上几个例子的执行结果:
本节中主要学习了linq中最基本的几个函数和语法,注意lambda和linq可以混用,当然lambda有时显然要比linq要方便很多