如何获得IGrouping的计数<' a,Foo> LINQ

时间:2018-04-28 16:01:59

标签: c# linq

我正在尝试编写一个返回按OrderID和DateTime值分组的列表的查询。我的第一个查询返回一个类型为

的分组计数
IGrouping<'a, Foo> SomeList

现在我试图执行一个循环来生成基于上面的类型List<ViewModel>模型的视图模型。问题是Order可能包含多个项OrderLines,我想添加一项检查,如果计数是&gt; 1然后遍历子项并创建一个对象以添加到List对象。

var query = (from o in db.Orders
             join ol in db.OrderLines on o.OrderID equals ol.OrderID
             join u in db.Users on o.UserSalesID equals u.UserID
             join r in db.Reports on o.UserSalesID equals r.UserId
             where o.Timestamp <= timeNow && o.Timestamp >= timeYesterday && u.UserID == o.UserSalesID
             group ol by new { o.Timestamp, o.OrderID } into g
             select g).ToList();

//循环

foreach (var orderLineList in query)
{
    foreach (var item in orderLineList) // how to get a count of OrderLineList here
    {
        // check if count is more than 1 then create an annon object and get total price of order etc 
        // if not then carry on as normal

        model.Add(new ReportViewModel()
        {
            Gross = orderLineList.Sum(ol => ol.RetailPrice),
            Net = orderLineList.Sum(ol => ol.RetailPrice), // minus expense,
            PaymentType = orders.Where(o => o.OrderID == item.OrderID).FirstOrDefault().PaymentID,
            Quantity = db.OrderLines.Where(ol => ol.OrderID == item.OrderID).Sum(ol => ol.Quantity),
            OrderID = item.OrderID
        });
    }
}

0 个答案:

没有答案