Linq:Grouping Operators

 1        [Category("Grouping Operators")]
 2             [Description("This sample uses group by to partition a list of numbers by " +
 3                         "their remainder when divided by 5.")]
 4             public void DataSetLinq40()
 5             {
 6 
 7                 var numbers = testDS.Tables["Numbers"].AsEnumerable();//testDS DataSet
 8                 foreach (var n in numbers)
 9                 {
10                     Console.Write(n.Field<int>("number")+",");
11                 }
12                 Console.WriteLine();
13                 var numberGroups =
14                     from n in numbers
15                     group n by n.Field<int>("number") % 5 into g
16                     select new { Remainder = g.Key, Numbers = g };
17 
18                 foreach (var g in numberGroups)
19                 {
20                     Console.WriteLine("Numbers with a remainder of {0} when divided by 5:", g.Remainder);
21                     foreach (var n in g.Numbers)
22                     {
23                         Console.WriteLine(n.Field<int>("number"));
24                     }
25                 }
26                 Console.ReadLine();
27             }

运行结果(按照数字求余5的值分组):

 1             [Category("Grouping Operators")]
 2             [Description("This sample uses group by to partition a list of words by " +
 3                          "their first letter.")]
 4             public void DataSetLinq41()
 5             {
 6 
 7                 var words4 = testDS.Tables["Words4"].AsEnumerable();
 8                 foreach (var g in words4)
 9                 {
10                     Console.Write(g.Field<string>("word")+",");
11                 }
12                 Console.WriteLine();
13                 var wordGroups =
14                     from w in words4
15                     group w by w.Field<string>("word")[0] into g
16                     select new { FirstLetter = g.Key, Words = g };
17 
18                 foreach (var g in wordGroups)
19                 {
20                     Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);
21                     foreach (var w in g.Words)
22                     {
23                         Console.WriteLine(w.Field<string>("word"));
24                     }
25                 }
26                 Console.ReadLine();
27             }

运行结果(按照首字母分组):

 1             [Category("Grouping Operators")]
 2             [Description("This sample uses group by to partition a list of products by category.")]
 3             public void DataSetLinq42()
 4             {
 5 
 6                 var products = testDS.Tables["Products"].AsEnumerable();
 7 
 8                 var productGroups =
 9                     from p in products
10                     group p by p.Field<string>("Category") into g
11                     select new { Category = g.Key, Products = g };
12 
13                 foreach (var g in productGroups)
14                 {
15                     Console.WriteLine("Category: {0}", g.Category);
16                     foreach (var w in g.Products)
17                     {
18                         Console.WriteLine("	" + w.Field<string>("ProductName"));
19                     }
20                 }
21                 Console.ReadLine();
22             }

运行结果(按照产品类别分组):

 1             [Category("Grouping Operators")]
 2             [Description("This sample uses group by to partition a list of each customer's orders, " +
 3                          "first by year, and then by month.")]
 4             public void DataSetLinq43()
 5             {
 6 
 7                 var customers = testDS.Tables["Customers"].AsEnumerable();
 8 
 9                 var customerOrderGroups =
10                     from c in customers
11                     select
12                         new
13                         {
14                             CompanyName = c.Field<string>("CompanyName"),
15                             YearGroups =
16                                 from o in c.GetChildRows("CustomersOrders")
17                                 group o by o.Field<DateTime>("OrderDate").Year into yg
18                                 select
19                                     new
20                                     {
21                                         Year = yg.Key,
22                                         MonthGroups =
23                                             from o in yg
24                                             group o by o.Field<DateTime>("OrderDate").Month into mg
25                                             select new { Month = mg.Key, Orders = mg }
26                                     }
27                         };
28 
29                 foreach (var cog in customerOrderGroups)
30                 {
31                     Console.WriteLine("CompanyName= {0}", cog.CompanyName);
32                     foreach (var yg in cog.YearGroups)
33                     {
34                         Console.WriteLine("	 Year= {0}", yg.Year);
35                         foreach (var mg in yg.MonthGroups)
36                         {
37                             Console.WriteLine("		 Month= {0}", mg.Month);
38                             foreach (var order in mg.Orders)
39                             {
40                                 Console.WriteLine("			 OrderID= {0} ", order.Field<int>("OrderID"));
41                                 Console.WriteLine("			 OrderDate= {0} ", order.Field<DateTime>("OrderDate"));
42                             }
43                         }
44                     }
45                 }
46                 Console.ReadLine();
47             }

DataTable:

运行结果(对每个客户的订单按年再按月分组):

 1 [Category("Grouping Operators")]
 2             [Description("This sample uses GroupBy to partition trimmed elements of an array using " +
 3                          "a custom comparer that matches words that are anagrams of each other.")]
 4             public void DataSetLinq44()
 5             {
 6 
 7                 var anagrams = testDS.Tables["Anagrams"].AsEnumerable();
 8 
 9                 var orderGroups = anagrams.GroupBy(w => w.Field<string>("anagram").Trim(), new AnagramEqualityComparer());
10 
11                 foreach (var g in orderGroups)
12                 {
13                     Console.WriteLine("Key: {0}", g.Key);
14                     foreach (var w in g)
15                     {
16                         Console.WriteLine("	" + w.Field<string>("anagram"));
17                     }
18                 }
19             }
20 
21             [Category("Grouping Operators")]
22             [Description("This sample uses GroupBy to partition trimmed elements of an array using " +
23                          "a custom comparer that matches words that are anagrams of each other, " +
24                          "and then converts the results to uppercase.")]
25             public void DataSetLinq45()
26             {
27 
28                 var anagrams = testDS.Tables["Anagrams"].AsEnumerable();
29 
30                 var orderGroups = anagrams.GroupBy(
31                     w => w.Field<string>("anagram").Trim(),
32                     a => a.Field<string>("anagram").ToUpper(),
33                     new AnagramEqualityComparer()
34                     );
35 
36                 foreach (var g in orderGroups)
37                 {
38                     Console.WriteLine("Key: {0}", g.Key);
39                     foreach (var w in g)
40                     {
41                         Console.WriteLine("	" + w);
42                     }
43                 }
44             }

内容源自:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

原文地址:https://www.cnblogs.com/yf2011/p/3371666.html