public static List <StoreMoveAggregationEntity> AggregateStoreMove(CompositeFilterDescriptorCollection filters) { var lp = _query.LinqOP; var brandIDs = VMGlobal.PoweredBrands.Select(o => o.ID); var storemoveContext = lp.Search <BillStoreMove>(o => o.OrganizationID == VMGlobal.CurrentUser.OrganizationID); FilterBillWithBrand(storemoveContext, filters, brandIDs); var detailsContext = lp.GetDataContext <BillStoreMoveDetails>(); var productContext = lp.GetDataContext <ViewProduct>(); var data = from sm in storemoveContext from details in detailsContext where sm.ID == details.BillID from product in productContext where product.ProductID == details.ProductID //&& brandIDs.Contains(product.BrandID) select new StoreMoveEntityForAggregation { ID = sm.ID, ProductID = product.ProductID, BrandID = product.BrandID, Code = sm.Code, CreateTime = sm.CreateTime.Date, StorageIDIn = sm.StorageIDIn, StorageIDOut = sm.StorageIDOut, StyleCode = product.StyleCode, Quantity = details.Quantity }; var filtedData = (IQueryable <StoreMoveEntityForAggregation>)data.Where(filters); var sum = filtedData.GroupBy(o => new { o.ProductID, o.StorageIDOut, o.StorageIDIn }).Select(g => new { Key = g.Key, Quantity = g.Sum(o => o.Quantity) }).ToList(); var pids = sum.Select(o => o.Key.ProductID).ToArray(); var products = _query.LinqOP.Search <ViewProduct>(o => pids.Contains(o.ProductID)).ToList(); var result = sum.Select(o => { var product = products.First(p => p.ProductID == o.Key.ProductID); return(new StoreMoveAggregationEntity { BYQID = product.BYQID, ProductID = product.ProductID, ProductCode = product.ProductCode, StyleCode = product.StyleCode, ColorID = product.ColorID, SizeID = product.SizeID, Quantity = o.Quantity, OutStorageName = Storages.Find(s => s.ID == o.Key.StorageIDOut).Name, InStorageName = Storages.Find(s => s.ID == o.Key.StorageIDIn).Name }); }).ToList(); foreach (var r in result) { r.ColorCode = VMGlobal.Colors.Find(o => o.ID == r.ColorID).Code; r.BrandID = VMGlobal.BYQs.Find(o => o.ID == r.BYQID).BrandID; r.BrandCode = VMGlobal.PoweredBrands.Find(o => o.ID == r.BrandID).Code; r.SizeName = VMGlobal.Sizes.Find(o => o.ID == r.SizeID).Name; } return(result); }