ImageVerifierCode 换一换
格式:DOCX , 页数:63 ,大小:33KB ,
资源ID:6216676      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6216676.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(101个LINQ例子.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

101个LINQ例子.docx

1、101个LINQ例子101个LINQ 例子Restriction Operators Where - Simple 1 public void Linq1() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var lowNums = from n in numbers where n 5 select n; Console.WriteLine(Numbers 5:); foreach (var x in lowNums) Console.WriteLine(x); Where - Simple 2 public void Linq2() List p

2、roducts = GetProductList(); var soldOutProducts = from p in products where p.UnitsInStock = 0 select p; Console.WriteLine(Sold out products:); foreach (var product in soldOutProducts) Console.WriteLine(0 is sold out!, product.ProductName); Where - Simple 3 public void Linq3() List products = GetProd

3、uctList(); var expensiveInStockProducts = from p in products where p.UnitsInStock 0 & p.UnitPrice 3.00M select p; Console.WriteLine(In-stock products that cost more than 3.00:); foreach (var product in expensiveInStockProducts) Console.WriteLine(0 is in stock and costs more than 3.00., product.Produ

4、ctName); Where - Drilldown public void Linq4() List customers = GetCustomerList(); var waCustomers = from c in customers where c.Region = WA select c; Console.WriteLine(Customers from Washington and their orders:); foreach (var customer in waCustomers) Console.WriteLine(Customer 0: 1, customer.Custo

5、merID, customer.CompanyName); foreach (var order in customer.Orders) Console.WriteLine( Order 0: 1, order.OrderID, order.OrderDate); Where - Indexed public void Linq5() string digits = zero, one, two, three, four, five, six, seven, eight, nine ; var shortDigits = digits.Where(digit, index) = digit.L

6、ength index); Console.WriteLine(Short digits:); foreach (var d in shortDigits) Console.WriteLine(The word 0 is shorter than its value., d); Projection Operators Select - Simple 1 public void Linq6() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var numsPlusOne = from n in numbers select n + 1; Consol

7、e.WriteLine(Numbers + 1:); foreach (var i in numsPlusOne) Console.WriteLine(i); Select - Simple 2 public void Linq7() List products = GetProductList(); var productNames = from p in products select p.ProductName; Console.WriteLine(Product Names:); foreach (var productName in productNames) Console.Wri

8、teLine(productName); Select - Transformation public void Linq8() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; string strings = zero, one, two, three, four, five, six, seven, eight, nine ; var textNums = from n in numbers select stringsn; Console.WriteLine(Number strings:); foreach (var s in textNums

9、) Console.WriteLine(s); Select - Anonymous Types 1public void Linq9() string words = aPPLE, BlUeBeRrY, cHeRry ; var upperLowerWords = from w in words select new Upper = w.ToUpper(), Lower = w.ToLower() ; foreach (var ul in upperLowerWords) Console.WriteLine(Uppercase: 0, Lowercase: 1, ul.Upper, ul.L

10、ower); Select - Anonymous Types 2 public void Linq10() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; string strings = zero, one, two, three, four, five, six, seven, eight, nine ; var digitOddEvens = from n in numbers select new Digit = stringsn, Even = (n % 2 = 0) ; foreach (var d in digitOddEvens) C

11、onsole.WriteLine(The digit 0 is 1., d.Digit, d.Even ? even : odd); Select - Anonymous Types 3 public void Linq11() List products = GetProductList(); var productInfos = from p in products select new p.ProductName, p.Category, Price = p.UnitPrice ; Console.WriteLine(Product Info:); foreach (var produc

12、tInfo in productInfos) Console.WriteLine(0 is in the category 1 and costs 2 per unit., productInfo.ProductName, productInfo.Category, productInfo.Price); Select - Indexedpublic void Linq12() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var numsInPlace = numbers.Select(num, index) = new Num = num,InP

13、lace = (num = index) ); Console.WriteLine(Number: In-place?); foreach (var n in numsInPlace) Console.WriteLine(0: 1, n.Num, n.InPlace); Select - Filteredpublic void Linq13() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; string digits = zero, one, two, three, four, five, six, seven, eight, nine ; var

14、lowNums = from n in numbers where n 5 select digitsn; Console.WriteLine(Numbers 5:); foreach (var num in lowNums) Console.WriteLine(num); SelectMany - Compound from 1public void Linq14() int numbersA = 0, 2, 4, 5, 6, 8, 9 ; int numbersB = 1, 3, 5, 7, 8 ; var pairs = from a in numbersA from b in numb

15、ersB where a b select newa, b; Console.WriteLine(Pairs where a b:); foreach (var pair in pairs) Console.WriteLine(0 is less than 1, pair.a, pair.b); SelectMany - Compound from 2 public void Linq15() List customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.Total

16、 500.00M select new c.CustomerID, o.OrderID, o.Total; ObjectDumper.Write(orders); SelectMany - Compound from 3 public void Linq16() List customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.OrderDate = new DateTime(1998, 1, 1) select new c.CustomerID, o.OrderID,

17、 o.OrderDate ; ObjectDumper.Write(orders); SelectMany - from Assignmentpublic void Linq17() List customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.Total = 2000.0M select new c.CustomerID, o.OrderID, o.Total; ObjectDumper.Write(orders); SelectMany - Multiple f

18、rompublic void Linq18() List customers = GetCustomerList(); DateTime cutoffDate = new DateTime(1997, 1, 1); var orders = from c in customers where c.Region = WA from o in c.Orders where o.OrderDate = cutoffDate select new c.CustomerID, o.OrderID ; ObjectDumper.Write(orders); SelectMany - Indexedpubl

19、ic void Linq19() List customers = GetCustomerList(); var customerOrders = customers.SelectMany( (cust, custIndex) = cust.Orders.Select(o = Customer # + (custIndex + 1) + has an order with OrderID + o.OrderID); ObjectDumper.Write(customerOrders); Partitioning Operators Take - Simple public void Linq2

20、0() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var first3Numbers = numbers.Take(3); Console.WriteLine(First 3 numbers:); foreach (var n in first3Numbers) Console.WriteLine(n); Take - Nestedpublic void Linq21() List customers = GetCustomerList(); var first3WAOrders = ( from c in customers from o in

21、 c.Orders where c.Region = WA select new c.CustomerID, o.OrderID, o.OrderDate ) .Take(3); Console.WriteLine(First 3 orders in WA:); foreach (var order in first3WAOrders) ObjectDumper.Write(order); Skip - Simplepublic void Linq22() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var allButFirst4Numbers

22、= numbers.Skip(4); Console.WriteLine(All but first 4 numbers:); foreach (var n in allButFirst4Numbers) Console.WriteLine(n); Skip - Nestedpublic void Linq23() List customers = GetCustomerList(); var waOrders = from c in customers from o in c.Orders where c.Region = WA select new c.CustomerID, o.Orde

23、rID, o.OrderDate ; var allButFirst2Orders = waOrders.Skip(2); Console.WriteLine(All but first 2 orders in WA:); foreach (var order in allButFirst2Orders) ObjectDumper.Write(order); TakeWhile - Simple public void Linq24() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var firstNumbersLessThan6 = number

24、s.TakeWhile(n = n n = index); Console.WriteLine(First numbers not less than their position:); foreach (var n in firstSmallNumbers) Console.WriteLine(n); /由 3 整除的第一个元素从开始的所有元素 3, 9, 8, 6, 7, 2, 0SkipWhile - Simplepublic void Linq26() int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var allButFirst3Number

25、s = numbers.SkipWhile(n = n % 3 != 0); Console.WriteLine(All elements starting from first element divisible by 3:); foreach (var n in allButFirst3Numbers) Console.WriteLine(n); SkipWhile - Indexedpublic void Linq27() /1, 3, 9, 8, 6, 7, 2, 0 int numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ; var laterNumbe

26、rs = numbers.SkipWhile(n, index) = n = index); Console.WriteLine(All elements starting from first element less than its position:); foreach (var n in laterNumbers) Console.WriteLine(n); Ordering Operators OrderBy - Simple 1 public void Linq28() string words = cherry, apple, blueberry ; var sortedWor

27、ds = from w in words orderby w select w; Console.WriteLine(The sorted list of words:); foreach (var w in sortedWords) Console.WriteLine(w); OrderBy - Simple 2 public void Linq29() string words = cherry, apple, blueberry ; var sortedWords = from w in words orderby w.Length select w; Console.WriteLine(The sorted list of words (by length):); foreach (var w in sortedWords) Console.WriteLine(w); OrderBy - Simple

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

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