101个LINQ例子.docx

上传人:b****5 文档编号:6216676 上传时间:2023-01-04 格式:DOCX 页数:63 大小:33KB
下载 相关 举报
101个LINQ例子.docx_第1页
第1页 / 共63页
101个LINQ例子.docx_第2页
第2页 / 共63页
101个LINQ例子.docx_第3页
第3页 / 共63页
101个LINQ例子.docx_第4页
第4页 / 共63页
101个LINQ例子.docx_第5页
第5页 / 共63页
点击查看更多>>
下载资源
资源描述

101个LINQ例子.docx

《101个LINQ例子.docx》由会员分享,可在线阅读,更多相关《101个LINQ例子.docx(63页珍藏版)》请在冰豆网上搜索。

101个LINQ例子.docx

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()

{

Listproducts=GetProductList();

varsoldOutProducts=

frompinproducts

wherep.UnitsInStock==0

selectp;

Console.WriteLine("Soldoutproducts:

");

foreach(varproductinsoldOutProducts)

{

Console.WriteLine("{0}issoldout!

",product.ProductName);

}

}

∙Where-Simple3

publicvoidLinq3()

{

Listproducts=GetProductList();

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()

{

Listcustomers=GetCustomerList();

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()

{

Listproducts=GetProductList();

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()

{

Listcustomers=GetCustomerList();

varorders=

fromcincustomers

fromoinc.Orders

whereo.Total<500.00M

selectnew{c.CustomerID,o.OrderID,o.Total};

ObjectDumper.Write(orders);

}

SelectMany-Compoundfrom3

publicvoidLinq16()

{

Listcustomers=GetCustomerList();

varorders=

fromcincustomers

fromoinc.Orders

whereo.OrderDate>=newDateTime(1998,1,1)

selectnew{c.CustomerID,o.OrderID,o.OrderDate};

ObjectDumper.Write(orders);

}

SelectMany-fromAssignment

publicvoidLinq17()

{

Listcustomers=GetCustomerList();

varorders=

fromcincustomers

fromoinc.Orders

whereo.Total>=2000.0M

selectnew{c.CustomerID,o.OrderID,o.Total};

ObjectDumper.Write(orders);

}

SelectMany-Multiplefrom

publicvoidLinq18()

{

Listcustomers=GetCustomerList();

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()

{

Listcustomers=GetCustomerList();

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()

{

Listcustomers=GetCustomerList();

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()

{

Listcustomers=GetCustomerList();

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1