LINQ 使用方法
2011-07-04 01:04
211 查看
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; List<Product> products = GetProductList(); var soldOutProducts = from p in products where p.UnitsInStock == 0 select p; List<Product> products = GetProductList(); var expensiveInStockProducts = from p in products where p.UnitsInStock > 0 && p.UnitPrice > 3.00M select p; List<Customer> customers = GetCustomerList(); var waCustomers = from c in customers where c.Region == "WA" select c; string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index); int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsPlusOne = from n in numbers select n + 1; List<Product> products = GetProductList(); var productNames = from p in products select p.ProductName; 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 strings ; string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; var upperLowerWords = from w in words select new { Upper = w.ToUpper(), Lower = w.ToLower() }; 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 = strings , Even = (n % 2 == 0) }; int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) }); 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 lowNums = from n in numbers where n < 5 select digits ; 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 numbersB where a < b select new { a, b }; List<Customer> customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.Total < 500.00M select new { c.CustomerID, o.OrderID, o.Total }; List<Customer> 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, o.OrderDate }; List<Customer> 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 }; List<Customer> 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 }; List<Customer> customers = GetCustomerList(); var customerOrders = customers.SelectMany( (cust, custIndex) => cust.Orders.Select(o => "Customer #" + (custIndex + 1) + " has an order with OrderID " + o.OrderID)); int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var first3Numbers = numbers.Take(3); int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst4Numbers = numbers.Skip(4); All but first 4 numbers: 9 8 6 7 2 0 List<Customer> customers = GetCustomerList(); var waOrders = from c in customers from o in c.Orders where c.Region == "WA" select new { c.CustomerID, o.OrderID, o.OrderDate }; var allButFirst2Orders = waOrders.Skip(2); int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6); int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index); int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0); 3 9 8 6 7 2 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var laterNumbers = numbers.SkipWhile((n, index) => n >= index); string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; var wordGroups = from w in words group w by w[0] into g select new { FirstLetter = g.Key, Words = g }; foreach (var g in wordGroups) { Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter); foreach (var w in g.Words) { Console.WriteLine(w); } } List<Product> products = GetProductList(); var orderGroups = from p in products group p by p.Category into g select new { Category = g.Key, Products = g }; ObjectDumper.Write(orderGroups, 1); List<Customer> customers = GetCustomerList(); var customerOrderGroups = from c in customers select new { c.CompanyName, YearGroups = from o in c.Orders group o by o.OrderDate.Year into yg select new { Year = yg.Key, MonthGroups = from o in yg group o by o.OrderDate.Month into mg select new { Month = mg.Key, Orders = mg } } };GroupBy - Comparer
This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other. [code]1. public void Linq44()
2. {
3. string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
4.
5. var orderGroups = anagrams.GroupBy(w => w.Trim(), new AnagramEqualityComparer());
6.
7. ObjectDumper.Write(orderGroups, 1);
8. }
9.
10. public class AnagramEqualityComparer : IEqualityComparer<string>
11. {
12. public bool Equals(string x, string y)
13. {
14. return getCanonicalString(x) == getCanonicalString(y);
15. }
16.
17. public int GetHashCode(string obj)
18. {
19. return getCanonicalString(obj).GetHashCode();
20. }
21.
22. private string getCanonicalString(string word)
23. {
24. char[] wordChars = word.ToCharArray();
25. Array.Sort<char>(wordChars);
26. return new string(wordChars);
27. }
28. }
Result
...
from
form
...
salt
last
...
earn
near
GroupBy - Comparer, Mapped
This sample uses GroupBy to partition trimmedelements of an array using a custom comparer that matches words that are
anagrams of each other, and then converts the results to uppercase.
1. public void Linq45()
2. {
3. string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
4.
5. var orderGroups = anagrams.GroupBy(
6. w => w.Trim(),
7. a => a.ToUpper(),
8. new AnagramEqualityComparer()
9. );
10.
11. ObjectDumper.Write(orderGroups, 1);
12. }
13.
14. public class AnagramEqualityComparer : IEqualityComparer<string>
15. {
16. public bool Equals(string x, string y)
17. {
18. return getCanonicalString(x) == getCanonicalString(y);
19. }
20.
21. public int GetHashCode(string obj)
22. {
23. return getCanonicalString(obj).GetHashCode();
24. }
25.
26. private string getCanonicalString(string word)
27. {
28. char[] wordChars = word.ToCharArray();
29. Array.Sort<char>(wordChars);
30. return new string(wordChars);
31. }
32. }
Result
...
FROM
FORM
...
SALT
LAST
...
EARN
NEAR
[/code]
相关文章推荐
- Linq使用之标准运算符方法
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL
- LINQ下使用三层架构的探索(四)建立一个逻辑访问层,并且添加一些查询方法
- 初步学习Linq的使用方法表达式
- 使用Linq中的Distinct方法对序列进行去重操作
- LINQ to Entities查询的简便方法就是使用函数
- Linq 常用方法使用总结
- 使用Linq求和方法Sum计算集合中多个元素和时应该注意的性能问题
- C# IEqualityComparer 使用方法 Linq Distinct使用方法
- C#使用linq对数组进行筛选排序的方法
- 如何很好的使用Linq的Distinct方法
- 在使用Linq的过程中,如果要进行数据的比较和处理,请记住使用ToList()方法。
- 怎么使用LINQ方法来比较自定义类型对象
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL 推荐
- Linq中GroupBy方法的使用总结
- Linq中几个扩展方法的使用
- 如何使用Linq或EF来对数据去重——Distinct方法详解
- 使用Serialize.Linq实现WCF方法参数可传入Linq
- 使用Linq的Intersect与Except方法的实例
- 使用Linq的Intersect与Except方法的实例