101个LINQ例子.docx
- 文档编号:6216676
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:63
- 大小:33KB
101个LINQ例子.docx
《101个LINQ例子.docx》由会员分享,可在线阅读,更多相关《101个LINQ例子.docx(63页珍藏版)》请在冰豆网上搜索。
101个LINQ例子
101个LINQ例子
RestrictionOperators
∙Where-Simple1
publicvoidLinq1()
{
int[]numbers={5,4,1,3,9,8,6,7,2,0};
varlowNums=
fromninnumbers
wheren<5
selectn;
Console.WriteLine("Numbers<5:
");
foreach(varxinlowNums)
{
Console.WriteLine(x);
}
}
∙Where-Simple2
publicvoidLinq2()
{
List
varsoldOutProducts=
frompinproducts
wherep.UnitsInStock==0
selectp;
Console.WriteLine("Soldoutproducts:
");
foreach(varproductinsoldOutProducts)
{
Console.WriteLine("{0}issoldout!
",product.ProductName);
}
}
∙Where-Simple3
publicvoidLinq3()
{
List
varexpensiveInStockProducts=
frompinproducts
wherep.UnitsInStock>0&&p.UnitPrice>3.00M
selectp;
Console.WriteLine("In-stockproductsthatcostmorethan3.00:
");
foreach(varproductinexpensiveInStockProducts)
{
Console.WriteLine("{0}isinstockandcostsmorethan3.00.",product.ProductName);
}
}
∙Where-Drilldown
publicvoidLinq4()
{
List
varwaCustomers=
fromcincustomers
wherec.Region=="WA"
selectc;
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-Indexed
publicvoidLinq5()
{
string[]digits={"zero","one","two","three","four","five","six","seven","eight","nine"};
varshortDigits=digits.Where((digit,index)=>digit.Length Console.WriteLine("Shortdigits: "); foreach(vardinshortDigits) { Console.WriteLine("Theword{0}isshorterthanitsvalue.",d); } } ProjectionOperators ∙Select-Simple1 publicvoidLinq6() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varnumsPlusOne= fromninnumbers selectn+1; Console.WriteLine("Numbers+1: "); foreach(variinnumsPlusOne) { Console.WriteLine(i); } } ∙Select-Simple2 publicvoidLinq7() { List varproductNames= frompinproducts selectp.ProductName; Console.WriteLine("ProductNames: "); foreach(varproductNameinproductNames) { Console.WriteLine(productName); } } ∙Select-Transformation publicvoidLinq8() { 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= fromninnumbers selectstrings[n]; Console.WriteLine("Numberstrings: "); foreach(varsintextNums) { Console.WriteLine(s); } } Select-AnonymousTypes1 publicvoidLinq9() { string[]words={"aPPLE","BlUeBeRrY","cHeRry"}; varupperLowerWords= fromwinwords selectnew{Upper=w.ToUpper(),Lower=w.ToLower()}; foreach(varulinupperLowerWords) { Console.WriteLine("Uppercase: {0},Lowercase: {1}",ul.Upper,ul.Lower); } } Select-AnonymousTypes2 publicvoidLinq10() { 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= fromninnumbers selectnew{Digit=strings[n],Even=(n%2==0)}; foreach(vardindigitOddEvens) { Console.WriteLine("Thedigit{0}is{1}.",d.Digit,d.Even? "even": "odd"); } } ∙Select-AnonymousTypes3 publicvoidLinq11() { Listproducts=GetProductList(); varproductInfos= frompinproducts selectnew{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-Indexed publicvoidLinq12() { 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-Filtered publicvoidLinq13() { 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= fromninnumbers wheren<5 selectdigits[n]; Console.WriteLine("Numbers<5: "); foreach(varnuminlowNums) { Console.WriteLine(num); } } ∙SelectMany-Compoundfrom1 publicvoidLinq14() { int[]numbersA={0,2,4,5,6,8,9}; int[]numbersB={1,3,5,7,8}; varpairs= fromainnumbersA frombinnumbersB wherea selectnew{a,b}; Console.WriteLine("Pairswherea "); foreach(varpairinpairs){ Console.WriteLine("{0}islessthan{1}",pair.a,pair.b); } } ∙SelectMany-Compoundfrom2 publicvoidLinq15() { List varorders= fromcincustomers fromoinc.Orders whereo.Total<500.00M selectnew{c.CustomerID,o.OrderID,o.Total}; ObjectDumper.Write(orders); } SelectMany-Compoundfrom3 publicvoidLinq16() { List varorders= fromcincustomers fromoinc.Orders whereo.OrderDate>=newDateTime(1998,1,1) selectnew{c.CustomerID,o.OrderID,o.OrderDate}; ObjectDumper.Write(orders); } SelectMany-fromAssignment publicvoidLinq17() { List varorders= fromcincustomers fromoinc.Orders whereo.Total>=2000.0M selectnew{c.CustomerID,o.OrderID,o.Total}; ObjectDumper.Write(orders); } SelectMany-Multiplefrom publicvoidLinq18() { List DateTimecutoffDate=newDateTime(1997,1,1); varorders= fromcincustomers wherec.Region=="WA" fromoinc.Orders whereo.OrderDate>=cutoffDate selectnew{c.CustomerID,o.OrderID}; ObjectDumper.Write(orders); } ∙SelectMany-Indexed publicvoidLinq19() { List varcustomerOrders= customers.SelectMany( (cust,custIndex)=> cust.Orders.Select(o=>"Customer#"+(custIndex+1)+ "hasanorderwithOrderID"+o.OrderID)); ObjectDumper.Write(customerOrders); } PartitioningOperators ∙Take-Simple publicvoidLinq20() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varfirst3Numbers=numbers.Take(3); Console.WriteLine("First3numbers: "); foreach(varninfirst3Numbers) { Console.WriteLine(n); } } ∙Take-Nested publicvoidLinq21() { List varfirst3WAOrders=( fromcincustomers fromoinc.Orders wherec.Region=="WA" selectnew{c.CustomerID,o.OrderID,o.OrderDate}) .Take(3); Console.WriteLine("First3ordersinWA: "); foreach(varorderinfirst3WAOrders) { ObjectDumper.Write(order); } } ∙Skip-Simple publicvoidLinq22() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varallButFirst4Numbers=numbers.Skip(4); Console.WriteLine("Allbutfirst4numbers: "); foreach(varninallButFirst4Numbers) { Console.WriteLine(n); } } ∙Skip-Nested publicvoidLinq23() { List varwaOrders= fromcincustomers fromoinc.Orders wherec.Region=="WA" selectnew{c.CustomerID,o.OrderID,o.OrderDate}; varallButFirst2Orders=waOrders.Skip (2); Console.WriteLine("Allbutfirst2ordersinWA: "); foreach(varorderinallButFirst2Orders) { ObjectDumper.Write(order); } } ∙TakeWhile-Simple publicvoidLinq24() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varfirstNumbersLessThan6=numbers.TakeWhile(n=>n<6); Console.WriteLine("Firstnumberslessthan6: "); foreach(varninfirstNumbersLessThan6) { Console.WriteLine(n); } } ∙TakeWhile–Indexed publicvoidLinq25() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varfirstSmallNumbers=numbers.TakeWhile((n,index)=>n>=index); Console.WriteLine("Firstnumbersnotlessthantheirposition: "); foreach(varninfirstSmallNumbers) { Console.WriteLine(n); } } //由3整除的第一个元素从开始的所有元素3,9,8,6,7,2,0 SkipWhile-Simple publicvoidLinq26() { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varallButFirst3Numbers=numbers.SkipWhile(n=>n%3! =0); Console.WriteLine("Allelementsstartingfromfirstelementdivisibleby3: "); foreach(varninallButFirst3Numbers) { Console.WriteLine(n); } } ∙SkipWhile-Indexed publicvoidLinq27()//1,3,9,8,6,7,2,0 { int[]numbers={5,4,1,3,9,8,6,7,2,0}; varlaterNumbers=numbers.SkipWhile((n,index)=>n>=index); Console.WriteLine("Allelementsstartingfromfirstelementlessthanitsposition: "); foreach(varninlaterNumbers) { Console.WriteLine(n); } } OrderingOperators ∙OrderBy-Simple1 publicvoidLinq28() { string[]words={"cherry","apple","blueberry"}; varsortedWords= fromwinwords orderbyw selectw; Console.WriteLine("Thesortedlistofwords: "); foreach(varwinsortedWords) { Console.WriteLine(w); } } ∙OrderBy-Simple2 publicvoidLinq29() { string[]words={"cherry","apple","blueberry"}; varsortedWords= fromwinwords orderbyw.Length selectw; Console.WriteLine("Thesortedlistofwords(bylength): "); foreach(varwinsortedWords) { Console.WriteLine(w); } } ∙OrderBy-Simple
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 101 LINQ 例子