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