101个LINQ例子说明:本文摘抄自Microsoft的MSDN网站,因为原文不方便快速阅读和学习,故此调整如下。本文所有权利归Microsoft公司所有。如果您觉得本文侵犯了您的著作权,请来信告知,我将即时改正!多谢!所有的代码运行结果请点击代码上的链接,到MSDN网站去看。RestrictionOperatorsWhere-Simple1publicvoidLinq1(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varlowNums=fromninnumberswheren5selectn;Console.WriteLine(Numbers5:);foreach(varxinlowNums){Console.WriteLine(x);}}Where-Simple2publicvoidLinq2(){Listproducts=GetProductList();varsoldOutProducts=frompinproductswherep.UnitsInStock==0selectp;Console.WriteLine(Soldoutproducts:);foreach(varproductinsoldOutProducts){Console.WriteLine({0}issoldout!,product.ProductName);}}Where-Simple3publicvoidLinq3(){Listproducts=GetProductList();varexpensiveInStockProducts=frompinproductswherep.UnitsInStock0&&p.UnitPrice3.00Mselectp;Console.WriteLine(In-stockproductsthatcostmorethan3.00:);foreach(varproductinexpensiveInStockProducts){Console.WriteLine({0}isinstockandcostsmorethan3.00.,product.ProductName);}}Where-DrilldownpublicvoidLinq4(){Listcustomers=GetCustomerList();varwaCustomers=fromcincustomerswherec.Region==WAselectc;Console.WriteLine(CustomersfromWashingtonandtheirorders:);foreach(varcustomerinwaCustomers){Console.WriteLine(Customer{0}:{1},customer.CustomerID,customer.CompanyName);foreach(varorderincustomer.Orders){Console.WriteLine(Order{0}:{1},order.OrderID,order.OrderDate);}}}Where-IndexedpublicvoidLinq5(){string[]digits={zero,one,two,three,four,five,six,seven,eight,nine};varshortDigits=digits.Where((digit,index)=digit.Lengthindex);Console.WriteLine(Shortdigits:);foreach(vardinshortDigits){Console.WriteLine(Theword{0}isshorterthanitsvalue.,d);}}ProjectionOperatorsSelect-Simple1publicvoidLinq6(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varnumsPlusOne=fromninnumbersselectn+1;Console.WriteLine(Numbers+1:);foreach(variinnumsPlusOne){Console.WriteLine(i);}}Select-Simple2publicvoidLinq7(){Listproducts=GetProductList();varproductNames=frompinproductsselectp.ProductName;Console.WriteLine(ProductNames:);foreach(varproductNameinproductNames){Console.WriteLine(productName);}}Select-TransformationpublicvoidLinq8(){int[]numbers={5,4,1,3,9,8,6,7,2,0};string[]strings={zero,one,two,three,four,five,six,seven,eight,nine};vartextNums=fromninnumbersselectstrings[n];Console.WriteLine(Numberstrings:);foreach(varsintextNums){Console.WriteLine(s);}}Select-AnonymousTypes1publicvoidLinq9(){string[]words={aPPLE,BlUeBeRrY,cHeRry};varupperLowerWords=fromwinwordsselectnew{Upper=w.ToUpper(),Lower=w.ToLower()};foreach(varulinupperLowerWords){Console.WriteLine(Uppercase:{0},Lowercase:{1},ul.Upper,ul.Lower);}}Select-AnonymousTypes2publicvoidLinq10(){int[]numbers={5,4,1,3,9,8,6,7,2,0};string[]strings={zero,one,two,three,four,five,six,seven,eight,nine};vardigitOddEvens=fromninnumbersselectnew{Digit=strings[n],Even=(n%2==0)};foreach(vardindigitOddEvens){Console.WriteLine(Thedigit{0}is{1}.,d.Digit,d.Even?even:odd);}}Select-AnonymousTypes3publicvoidLinq11(){Listproducts=GetProductList();varproductInfos=frompinproductsselectnew{p.ProductName,p.Category,Price=p.UnitPrice};Console.WriteLine(ProductInfo:);foreach(varproductInfoinproductInfos){Console.WriteLine({0}isinthecategory{1}andcosts{2}perunit.,productInfo.ProductName,productInfo.Category,productInfo.Price);}}Select-IndexedpublicvoidLinq12(){int[]numbers={5,4,1,3,9,8,6,7,2,0};varnumsInPlace=numbers.Select((num,index)=new{Num=num,InPlace=(num==index)});Console.WriteLine(Number:In-place?);foreach(varninnumsInPlace){Console.WriteLine({0}:{1},n.Num,n.InPlace);}}Select-FilteredpublicvoidLinq13(){int[]numbers={5,4,1,3,9,8,6,7,2,0};string[]digits={zero,one,two,three,four,five,six,seven,eight,nine};varlowNums=fromninnumberswheren5selectdigits[n];Console.WriteLine(Numbers5:);foreach(varnuminlowNums){Console.WriteLine(num);}}SelectMany-Compoundfrom1publicvoidLinq14(){int[]numbersA={0,2,4,5,6,8,9};int[]numbersB={1,3,5,7,8};varpairs=fromainnumbersA,binnumbersBwhereabselectnew{a,b};Console.WriteLine(Pairswhereab:);foreach(varpairinpairs){Console.WriteLine({0}islessthan{1},pair.a,pair.b);}}SelectMany-Compoundfrom2publicvoidLinq15(){Listcustomers=GetCustomerList();varorders=fromcincustomers,oinc.Orderswhereo.Total500.00Mselectnew{c.CustomerID,o.OrderID,o.Total};ObjectDumper.Write(orders);}SelectMany-Compoundfrom3publicvoidLinq16(){Listcustomers=GetCustomerList();varorders=fromcincustomers,oinc.Orderswhereo.OrderDate=newDateTime(1998,1,1)selectnew{c.CustomerID,o.OrderID,o.OrderDate};ObjectDumper.Write(orders);}SelectMany-fromAssignmentpublicvoidLinq17(){Listcustomers=GetCustomerList();varorders=fromcincustomers,oinc.Orders,total=o.Totalwheretotal=2000.0Mselectnew{c.CustomerID,o.OrderID,total};ObjectDumper.Write(orders);}SelectMany-MultiplefrompublicvoidLinq18(){Listcustomers=GetCustomerList();DateTimecutoffDate=newDateTime(1997,1,1);varorders=fromcincustomerswherec.Region==WAfromoinc.Orderswhereo.OrderDate=cutoffDateselectnew{c.CustomerID,o.OrderID};ObjectDumper.Write(orders);}SelectMany-IndexedpublicvoidLinq19(){Listcustomers=GetCustomerList();varcustomerOrders=custom