EF4中根据时间分组

现有下面Order实体,要根据其中的OrderDate进行分组

 

  代码如下:

代码

            using (var context = new EFDemoEntities())
            {
                var groups 
= from r in context.Orders
                             group r by EntityFunctions.TruncateTime(r.OrderDate)
                                 into g
                                 select g;
                
foreach (var element in groups)
                {
                    Console.WriteLine(
"{0}订单",
                    ((DateTime)element.Key).ToShortDateString());
                    
foreach (var order in element)
                    {
                        Console.WriteLine(
"\tOrderID:{0}  , OrderDate:{1}", order.OrderID , order.OrderDate);
                    }
                }
            }

 

 

结果如下:

 

 

 

posted @ 2010-07-14 22:21  i'm zjz  阅读(1593)  评论(0编辑  收藏  举报