C# 使用LINQ ForEach循环的替代方法是什么?
在我的13000药品案例中,这个代码需要8-10秒才能完成,但考虑到性能问题,它太长了。我如何优化这个 使用LINQ ForEach循环的替代方法是什么 一个标准的foreach 我如何优化这个C# 使用LINQ ForEach循环的替代方法是什么?,c#,linq,C#,Linq,在我的13000药品案例中,这个代码需要8-10秒才能完成,但考虑到性能问题,它太长了。我如何优化这个 使用LINQ ForEach循环的替代方法是什么 一个标准的foreach 我如何优化这个 关于性能,它不是你的代码>前缀这就是问题,它可能是选择< /COD>和包含< /代码>,考虑使用 ToHasSET/曾经 medicineList.ForEach(x => { Docto
关于性能,它不是你的代码>前缀这就是问题,它可能是<代码>选择< /COD>和<代码>包含< /代码>,考虑使用<代码> ToHasSET/<代码>曾经
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);
}
});
然后可以在循环中使用
var set = this.MedicineCategoryItemsCombo.Select(y => y.CategoryID).ToHashSet();
但是,在阅读代码时,可以使用更好的查询和Where
,然后执行选择
使用LINQ ForEach循环的替代方法是什么
一个标准的foreach
我如何优化这个
关于性能,它不是你的代码>前缀这就是问题,它可能是<代码>选择< /COD>和<代码>包含< /代码>,考虑使用<代码> ToHasSET/<代码>曾经
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);
}
});
然后可以在循环中使用
var set = this.MedicineCategoryItemsCombo.Select(y => y.CategoryID).ToHashSet();
但是,在阅读代码时,可能可以通过更好的查询和Where
,然后执行选择
更新来优化此操作:我有一些时间,因此我能够编写完整的示例:
结果是:
总结
使用进行转换后使用AddRange
。选择
在流程结束时使用Distinct
,而不是在每个循环中逐个扫描和添加
解决方案
公共静态列表MyWay(列表medicineList)
{
var temp=新列表();
var DrugItemsComboForSearch=新列表();
var=medicineList。选择(x=>
{
返回新的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();
温度范围(转换);
DrugItemsComboForSearch.AddRange(已转换);
var MedicineCategoryItemsCombo=transformed.Select(m=>(catId:m.CategoryID,catName:m.CategoryName)).Distinct().ToList();
退回药品类别清单;
}
完整示例:
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;
}
公共静态类MainClass
{
公立医学
{
公共字符串PKID{get;set;}
公共字符串名称{get;set;}
公共字符串UnitName{get;set;}
公共字符串CategoryID{get;set;}
公共字符串CategoryName{get;set;}
公共字符串DosageFormID{get;set;}
公共字符串InventoryTypeID{get;set;}
}
公共类DoctorOrderViewModel
{
公共字符串DrugID{get;set;}
公共字符串名称{get;set;}
公共字符串DrugName{get;set;}
公共字符串UnitName{get;set;}
公共字符串CategoryID{get;set;}
公共字符串CategoryName{get;set;}
公共字符串DosageFormID{get;set;}
公共字符串InventoryTypeID{get;set;}
}
类别
{
公共字符串CategoryID{get;set;}
}
公共静态void Main()
{
var=新列表();
medicines.AddRange(Enumerable.Range(01300)。选择(i=>newmedicine()
{
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,
}));
秒表sw=新秒表();
sw.Start();
列表数据=null;
对于(int i=0;i<10;i++)
{
comboData=OpWay(药物);
}
var经过=sw.经过;
WriteLine($“10x OPWay,用于{medicines.Count}药品和{comboData.Count}类别:{appeased}”);
sw.Restart();
列表comboData2=null;
对于(int i=0;i<10;i++)
{
comboData2=MyWay(药物);
}
经过=西南经过;
WriteLine($“10x MyWay,用于{medicines.Count}药品和{comboData2.Count}类别:{appeased}”);
}
公共静态列表选项(列表药物列表)
{
List MedicineCategoryItemsCombo=新列表();
var temp=新列表();
var DrugItemsComboForSearch=新列表();
medicineList.ForEach(x=>
{
DoctorOrderViewModel vm=新的DoctorOrderViewModel()
{
DrugID=x.PKID,
Name=x.Name,
DrugName=x.Name,
UnitName=x.UnitName,
CategoryID=x.CategoryID,
CategoryName=x.CategoryName,
DosageFormID=x.DosageFormID,
InventoryTypeID=x.InventoryTypeID,
};
临时添加(vm);
DrugItemsComboForSearch.Add(vm);
DoctorOrderViewModel vm2=新的DoctorOrderViewModel(){CategoryID=x.CategoryID,CategoryName=x.CategoryName,};
如果(!MedicineCategoryItemsCombo.Select(y=>y.CategoryID).Contains(x.CategoryID))
{
药品类别商品目录新增(vm2);
}
});
退回药品类别清单;
}
公共静态列表MyWay(列表medicineList)
{
var temp=新列表();
var DrugItemsComboForSearch=新列表();
var=medicineList。选择(x=>
{
返回新的DoctorsOrderViewModel()
{
DrugID=x.PKID,
Name=x.Name,
DrugName=x.Name,
UnitName=x.UnitName,
C
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;
}
}
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,
};);
}
}