如何通过linq循环遍历多级主数据?

时间:2015-01-21 04:59:46

标签: c# linq linq-to-objects

我有三个级别的主细节关系,一个购买可以包含多个challan,一个challan可以包含多个项目。每个项目都有一些数量。我需要计算每次购买的总项目数量。我已经通过以下方式完成了这项工作,但是只需要花费大量时间来处理少量数据。我担心当数据量变大时会发生什么。有没有办法在使用join或其他任何东西的单个查询中执行此操作?提前谢谢。

var allData = (from p in _context.Prq_Purchase.AsEnumerable()
                       //where p.RecordStatus == "NCF"
                       from s in _context.Sys_Supplier
                       where s.SupplierID == p.SupplierID
                       from sa in _context.Sys_SupplierAddress
                       where sa.SupplierAddressID == p.SupplierAddressID
                       orderby p.PurchaseID descending
                       select new PurchaseReceive
                       {
                           PurchaseID = (p.PurchaseID).ToString(),
                           PurchaseNo= p.PurchaseNo,
                           SupplierID = (p.SupplierID).ToString(),
                           SupplierName = s.SupplierName,
                           Address = sa.Address,
                           SupplierAddressID = (p.SupplierAddressID).ToString(),
                           PurchaseCategory = p.PurchaseCategory,
                           PurchaseType = p.PurchaseType,
                           PurchaseYear = p.PurchaseYear,
                           PurchaseDate = (p.PurchaseDate).ToString("dd'/'MM'/'yyyy"),
                           RecordStatus= DalCommon.ReturnRecordStatus(p.RecordStatus)
                       }).ToList();

        foreach(var Purchase in allData)
        {
            decimal TotalQty = 0;

            var ChallanList= (from c in _context.Prq_PurchaseChallan.AsEnumerable()
                              where (c.PurchaseID).ToString()==Purchase.PurchaseID
                              select c).ToList();



            foreach(var Challan in ChallanList)
            {
                var ItemList = (from i in _context.Prq_PurchaseChallanItem.AsEnumerable()
                                where i.ChallanID == Challan.ChallanID
                                select i).ToList();

                foreach(var Item in ItemList )
                {
                    TotalQty = TotalQty + Item.ReceiveQty;
                }

            }
            Purchase.TotalItem = TotalQty;
        }

0 个答案:

没有答案