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);
}
}
}
{
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);
}
}
}
结果如下: