我有一些定义如下的对象:
public class Customer
{
public int CustId { get; set; }
public string CustName{ get; set; }
}
public class Order
{
public DateTime OrderDate { get; set; }
public int CustId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
public int OrderId { get; set; }
}
public class DetailReport
{
public Customer TheCustomer { get; set; }
public IList<Orders> OrderList { get; set; }
}
出于报告目的,我得到一个List类型的对象,我从中创建一个表格报告。
IList<DetailReport> detailReport
一切顺利,直到我要求根据OrderDate生成图表并获得当天的订单总数。
所以现在我必须取出在该特定日期具有DateTime值(OrderDate)和订单ID计数的数据。
我可以执行以下操作吗?
from orderdata in detailReport.OrderList.........
我需要明天的详细报告列表
中的唯一日期和Order of OrderIds我怎样才能做到这一点?
答案 0 :(得分:1)
试试这个: -
var result = report.SelectMany(x => x.OrderList)
.GroupBy(x => x.OrderDate.Date)
.Select(x => new
{
OrderDate = x.Key,
OrderCount = x.Count()
});
请检查Fiddle我使用过的样本数据。
答案 1 :(得分:1)
你可以这样写:
var Result = (from reportdata in detailReport
from orderdata in reportdata.OrderList
group orderdata by orderdata.OrderDate.Date into g
select new
{
OrderDate = g.Key,
Count = g.Count()
}).ToList();
或利用SelectMany()
展平列表中的列表:
var Result = (from orderdata in detailReport.SelectMany(x=>>x.OrderList)
group orderdata by orderdata.OrderDate.Date into g
select new
{
OrderDate = g.Key,
Count = g.Count()
}).ToList();