5. 分组操作符—【LINQ标准查询操作符】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class GroupBy_LINQ
    {
        static string ContextString = System.Configuration.ConfigurationSettings.AppSettings["ContextString"].ToString();
        static DataContext context = new DataContext(ContextString);
        static Table<SalesOrderHeader> orders = context.GetTable<SalesOrderHeader>();
 
        public static void GroupBy_Print()
        {
            var groupByQuery = orders.Where(ord => ord.SalesPersonID > 0 && ord.SalesPersonID!=null).GroupBy(ord => ord.SalesPersonID, order => order.CustomerID);
 
            foreach (var item in groupByQuery)
            {
                Console.WriteLine(item.Key);
                foreach (var o in item)
                {
                    Console.WriteLine("   " + o.ToString());
                }
            }
 
            Console.ReadKey();
        }
 
        public static void GroupBy_Print2()
        {
            // 混合语法,因为GroupBy在查询语法中不能使用
            var groupByQuery = (from o in orders
                                where o.SalesPersonID > 0 && o.SalesPersonID != null
                                select o).GroupBy(ord => ord.SalesPersonID, ord => ord.CustomerID);               
 
            foreach (var item in groupByQuery)
            {
                Console.WriteLine(item.Key);
                foreach (var o in item)
                {
                    Console.WriteLine("   " + o.ToString());
                }
            }
 
            Console.ReadKey();
        }
    }
posted @   CityWalker  阅读(564)  评论(0编辑  收藏  举报
努力加载评论中...
天天来
点击右上角即可分享
微信分享提示