简体   繁体   English

在 C# 中使用虚拟集合使代码更高效

[英]Using virtual collection in C# to make code more efficient

Hello I have a model that looks like this,你好,我有一个看起来像这样的模型,

  public class Site
    {
        public int ID { get; set; }

        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public string PostalCode { get; set; }

        public virtual ICollection<OffSiteItemDetails> ItemDetails { get; private set; }
        public virtual ICollection<SalesOrder> soDetails { get; private set; }
        public virtual ICollection<SODetails> SODDetails { get; private set; }

        public string IncomingTotalOSI(Site s)
        {
            PIC_Program_1_0Context db = new PIC_Program_1_0Context();
            float? partCost = 0;
            float? componentCost = 0;
            float? itemCost = 0;
            float? incomingCost = 0;
            List<OffSiteItemDetails> d = ItemDetails.Where(x => x.siteID == s.ID).ToList();


            var less30days = DateTime.Now.AddDays(-30);
            //List<SalesOrder> so = soDetails.Where(x => x.OrderType == SOType.OffSiteInventory).Where(x => x.DateCreated > less30days).ToList();
            List<SalesOrder> so = db.SalesOrders.Where(x => x.OrderType == SOType.OffSiteInventory).Where(x => x.DateCreated > less30days).ToList();
            List<SODetails> sod = db.SODetails.Where(x => x.SiteID == s.ID).ToList();

       foreach (var order in so)
        {

            foreach (var details in sod)
            {

                if (order.ID == details.SalesOrderID)
                {

                    if (details.PartID != null){
                        partCost += details.part_qty * db.Parts.Where(x => x.ID == details.PartID).FirstOrDefault().AveCostPerUnit;
                    }
                    else if (details.ComponentID != null){
                        componentCost += details.comp_qty * db.Components.Where(x => x.ID == details.ComponentID).FirstOrDefault().AveCostPerUnit;
                    }
                    else{
                        itemCost += details.item_qty * db.Items.Where(x => x.ID == details.ItemID).FirstOrDefault().CostPerUnit;
                    }
                }

            }
        }
        incomingCost = partCost + componentCost + itemCost;

            string cost = String.Format("{0:C}", incomingCost);

            return cost;
        }
  }

But this is inefficient as it looping over the same data set multiple times.但这效率低下,因为它多次循环访问同一数据集。 And the more orders that are added, the slower the program will be become.并且添加的订单越多,程序就会变得越慢。 I'm still learning MVC and have looked into virtual collections but I'm still confused on how I could you it in this scenario to improve the efficiency of the program我仍在学习 MVC 并研究了虚拟集合,但我仍然对如何在这种情况下提高程序效率感到困惑

To begin with, I would suggest removing the .ToList() from this line:首先,我建议从这一行中删除.ToList()

List sod = db.SODetails.Where(x => x.SiteID == s.ID).ToList();

When you use ToList() there, you are forcing an execution and enumeration to occur against the underlying data store at that point rather than deferring execution until you actually need to enumerate the desired results.当您在那里使用 ToList() 时,您将在该点强制对基础数据存储进行执行和枚举,而不是推迟执行,直到您确实需要枚举所需的结果。

Then change the following line:然后更改以下行:

foreach (var details in sod)

To:到:

foreach (var details in db.SODetails .Where(x => x.SiteID == s.ID && details.SalesOrderID == order.ID))

The trade-off of course is that you are hitting the database with a query for every sales order, but you are not iterating over the same data multiple times.当然,权衡是您通过查询每个销售订单来访问数据库,但您不会多次迭代相同的数据。

You could also get rid of the if() statement in the inner most loop and simply put your condition in the Linq to limit what is looped over.您还可以去掉最内层循环中的 if() 语句,只需将您的条件放在 Linq 中以限制循环的内容。 This may improve performance as well:这也可以提高性能:

foreach (var details in sod.Where(detail => detail.SalesOrderID == order.ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM