Asp.net mvc 按LINQ ASP.NET MVC分组

Asp.net mvc 按LINQ ASP.NET MVC分组,asp.net-mvc,linq,Asp.net Mvc,Linq,假设我们有这样一个Linq查询: int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId); var stock = from i in _stockService.GetStock() join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id where ur.Comapn

假设我们有这样一个Linq查询:

int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId);

var stock = from i in _stockService.GetStock()
            join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id
            where ur.ComapnyId == companyID
            select new StockVM
                       {
                           Product = ur.ItemName,
                           Quantity = i.Quantity,
                           BatchID = i.BatchID,
                           StockStatus = i.StockStatus,
                           MfgDate = i.MfgDate,
                           ExpDate = i.ExpDate,
                       };
结果

在这个linq查询中,如何使用
数量
之和进行“按产品分组”


我只需要先获取max ExpDate或默认值,然后尝试以下操作:

int companyID = Convert.ToInt32(((UserIdentity)User.Identity).CompanyId);

var stock = from i in _stockService.GetStock()
            join ur in _inventoryService.GetInventory() on i.ProductID equals ur.Id
            where ur.ComapnyId == companyID
group new { Quantity = i.Quantity } by new { ur.ItemName } into g
select new { Product = g.Key, TotalQuantity = g.Sum() } ).ToList() ;

List<StockVM> _lst = new List<StockVM>();
foreach(var item in stock ){
   StockVM row = new StockVM();
   row.Product = item.ItemName;
   //....
   _lst.Add(row);
}
int companyID=Convert.ToInt32(((UserIdentity)User.Identity.companyID);
var stock=来自i in_stockService.GetStock()
将您加入i.ProductID上的_inventoryService.GetInventory()等于您的.Id
其中ur.companyID==companyID
按新的{ur.ItemName}将新的{Quantity=i.Quantity}分组到g中
选择新的{Product=g.Key,TotalQuantity=g.Sum()}).ToList();
列表_lst=新列表();
foreach(库存var项目){
StockVM行=新StockVM();
row.Product=item.ItemName;
//....
_第一次添加(行);
}

我假设您希望结果为产品:桌面会计软件数量:3 BatchId:12345 StockStatus:_nilMFGDate:3/29/2017 11:50:07 PM ExpDate:4/29/2017 11:50:07 PM看到您之前的问题,您可能认为您现在加入该联盟已经脱离困境了,但是,由于要加入两个
IEnumerable
s,这将严重影响性能。每次查询都会将整个stock表拉入内存。真的,除了重新设计,你别无选择。