使用LINQ ForEach循环的替代方法是什么?

时间:2018-09-05 03:35:10

标签: c# linq

 medicineList.ForEach(x =>
                         {
                             DoctorsOrderViewModel vm = new DoctorsOrderViewModel()
                             {
                                 DrugID = x.PKID,
                                 Name = x.Name,
                                 DrugName = x.Name,
                                 UnitName = x.UnitName,
                                 CategoryID = x.CategoryID,
                                 CategoryName = x.CategoryName,
                                 DosageFormID = x.DosageFormID,
                                 InventoryTypeID = x.InventoryTypeID,
                             };

                             temp.Add(vm);
                             this.DrugItemsComboForSearch.Add(vm);


                             DoctorsOrderViewModel vm2 = new DoctorsOrderViewModel() { CategoryID = x.CategoryID, CategoryName = x.CategoryName, };

                             if (!this.MedicineCategoryItemsCombo.Select(y => y.CategoryID).Contains(x.CategoryID))
                             {
                                 this.MedicineCategoryItemsCombo.Add(vm2);
                             }
                         });

在我的13000医学案例中,此代码花了8到10秒才能完成,但考虑到性能问题,时间太长。我该如何优化呢?

3 个答案:

答案 0 :(得分:4)

  

使用LINQ ForEach循环的替代方法是什么?

标准saveAsNewAPIHadoopDataset

  

我该如何优化

关于性能,这不是您的foreach,而是问题,可能是ForEachselect,请考虑一次使用contains

ToHashSet

然后您可以在循环中使用

var set = this.MedicineCategoryItemsCombo.Select(y => y.CategoryID).ToHashSet();

但是,在阅读代码时,可以使用更好的查询和if (set.Add(x.CategoryID)) { this.MedicineCategoryItemsCombo.Add(vm2); } 来优化此代码,然后执行Where

答案 1 :(得分:3)

更新:我花了一些时间,所以我能够写一个完整的例子:

结果:

10x OPWay for 13000 medicines and 1000 categories: 00:00:03.8986663
10x MyWay for 13000 medicines and 1000 categories: 00:00:00.0879221

摘要

  1. AddRange转换后使用.Select
  2. 在过程结束时使用Distinct,而不是在每个循环中逐一扫描并添加。

解决方案

    public static List<(string catId, string catName)> MyWay(List<Medicine> medicineList)
    {
        var temp = new List<DoctorsOrderViewModel>();
        var DrugItemsComboForSearch = new List<DoctorsOrderViewModel>();

        var transformed = medicineList.Select(x =>
        {
            return new DoctorsOrderViewModel()
            {
                DrugID = x.PKID,
                Name = x.Name,
                DrugName = x.Name,
                UnitName = x.UnitName,
                CategoryID = x.CategoryID,
                CategoryName = x.CategoryName,
                DosageFormID = x.DosageFormID,
                InventoryTypeID = x.InventoryTypeID,
            };

        }).ToList(); ;

        temp.AddRange(transformed);
        DrugItemsComboForSearch.AddRange(transformed);

        var MedicineCategoryItemsCombo = transformed.Select(m => (catId: m.CategoryID, catName: m.CategoryName)).Distinct().ToList();

        return MedicineCategoryItemsCombo;
    }

完整示例:

public static class MainClass
{
    public class Medicine
    {
        public string PKID { get; set; }
        public string Name { get; set; }
        public string UnitName { get; set; }

        public string CategoryID { get; set; }
        public string CategoryName { get; set; }
        public string DosageFormID { get; set; }
        public string InventoryTypeID { get; set; }
    }

    public class DoctorsOrderViewModel
    {
        public string DrugID { get; set; }
        public string Name { get; set; }
        public string DrugName { get; set; }
        public string UnitName { get; set; }

        public string CategoryID { get; set; }
        public string CategoryName { get; set; }
        public string DosageFormID { get; set; }
        public string InventoryTypeID { get; set; }
    }

    class Category
    {
        public string CategoryID { get; set; }
    }

    public static void Main()
    {

        var medicines = new List<Medicine>();

        medicines.AddRange(Enumerable.Range(0, 13000).Select(i => new Medicine()
        {
            PKID = "PKID" + i,
            Name = "Name" + i,
            UnitName = "UnitName" + i,
            CategoryID = "CategoryID" + i%1000,
            CategoryName = "CategoryName for CategoryID" + i%1000,
            DosageFormID = "DosageFormID" + i,
            InventoryTypeID = "InventoryTypeID" + i,
        }));

        Stopwatch sw = new Stopwatch();
        sw.Start();
        List<DoctorsOrderViewModel> comboData = null;
        for (int i = 0; i < 10; i++)
        {
            comboData = OpWay(medicines);
        }
        var elapsed = sw.Elapsed;

        Console.WriteLine($"10x OPWay for {medicines.Count} medicines and {comboData.Count} categories: {elapsed}");


        sw.Restart();
        List<(string catId, string catName)> comboData2 = null;
        for (int i = 0; i < 10; i++)
        {
            comboData2 = MyWay(medicines);
        }
        elapsed = sw.Elapsed;

        Console.WriteLine($"10x MyWay for {medicines.Count} medicines and {comboData2.Count} categories: {elapsed}");

    }

    public static List<DoctorsOrderViewModel> OpWay(List<Medicine> medicineList)
    {
        List<DoctorsOrderViewModel> MedicineCategoryItemsCombo = new List<DoctorsOrderViewModel>();

        var temp = new List<DoctorsOrderViewModel>();
        var DrugItemsComboForSearch = new List<DoctorsOrderViewModel>();

        medicineList.ForEach(x =>
        {
            DoctorsOrderViewModel vm = new DoctorsOrderViewModel()
            {
                DrugID = x.PKID,
                Name = x.Name,
                DrugName = x.Name,
                UnitName = x.UnitName,
                CategoryID = x.CategoryID,
                CategoryName = x.CategoryName,
                DosageFormID = x.DosageFormID,
                InventoryTypeID = x.InventoryTypeID,
            };

            temp.Add(vm);
            DrugItemsComboForSearch.Add(vm);


            DoctorsOrderViewModel vm2 = new DoctorsOrderViewModel() { CategoryID = x.CategoryID, CategoryName = x.CategoryName, };

            if (!MedicineCategoryItemsCombo.Select(y => y.CategoryID).Contains(x.CategoryID))
            {
                MedicineCategoryItemsCombo.Add(vm2);
            }
        });

        return MedicineCategoryItemsCombo;
    }

    public static List<(string catId, string catName)> MyWay(List<Medicine> medicineList)
    {
        var temp = new List<DoctorsOrderViewModel>();
        var DrugItemsComboForSearch = new List<DoctorsOrderViewModel>();

        var transformed = medicineList.Select(x =>
        {
            return new DoctorsOrderViewModel()
            {
                DrugID = x.PKID,
                Name = x.Name,
                DrugName = x.Name,
                UnitName = x.UnitName,
                CategoryID = x.CategoryID,
                CategoryName = x.CategoryName,
                DosageFormID = x.DosageFormID,
                InventoryTypeID = x.InventoryTypeID,
            };

        }).ToList(); ;

        temp.AddRange(transformed);
        DrugItemsComboForSearch.AddRange(transformed);

        var MedicineCategoryItemsCombo = transformed.Select(m => (catId: m.CategoryID, catName: m.CategoryName)).Distinct().ToList();

        return MedicineCategoryItemsCombo;
    }
}

答案 2 :(得分:-1)

您可以对foreach使用不同的方法,这比上面的方法更好,代码也可以最小化:

foreach (Medicine medicine in medicineList)
            {
                DoctorsOrderViewModel vm = new DoctorsOrderViewModel()
                {
                    DrugID = x.PKID,
                    Name = x.Name,
                    DrugName = x.Name,
                    UnitName = x.UnitName,
                    CategoryID = x.CategoryID,
                    CategoryName = x.CategoryName,
                    DosageFormID = x.DosageFormID,
                    InventoryTypeID = x.InventoryTypeID,
                };

                temp.Add(vm);
                this.DrugItemsComboForSearch.Add(vm);




                if (!this.MedicineCategoryItemsCombo.Select(y => y.CategoryID == 
                    x.CategoryID).Any())
                {
                    this.MedicineCategoryItemsCombo.Add(new DoctorsOrderViewModel()
                    {
                        CategoryID = x.CategoryID,
                        CategoryName = x.CategoryName,
                    };);
                }
            }