本文介绍了如何将正常的foreach循环转换为c#中的lambda表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 •public static Collection < SalesPlan > GetCurrentPlan(PurchasePlanRequest purchasePlanRequest) { SalesCustomerManager salesCustomerManager = new SalesCustomerManager(); SalesCustomer salesCustomer = new SalesCustomer(); salesCustomer.ReportingCustomerNumber = purchasePlanRequest.ReportingCustomerId; 集合< SalesPlan > salesPlanCollection = new Collection < SalesPlan > (); salesCustomer = salesCustomerManager.GetCustomerLinesPlansCoverages(salesCustomer); 集合< SalesPlan > renewalPlan = new Collection < SalesPlan > (); 集合< SalesPlan > currentPlan = new Collection < SalesPlan > (); foreach(SalesLineOfBusiness lob in salesCustomer.SalesLineOfBusinessCollection) { if(lob.LineOfBusinessCode == purchasePlanRequest.LOBCode) { foreach(SalesPlan salepln in lob.SalesPlanCollection) { //比较PlanEffectiveDate等于CurrentRenewalDate。 if(purchasePlanRequest.PlanEffectiveDate == currentRenewalDate) { if(salepln.IsRenewalPlan == true) { currentPlan.Add(salepln); } } 其他 { if(salepln.IsCurrentPlan == true) { currentPlan.Add(salepln); } } } } } return currentPlan; } 在粗体代码foreach循环中,我想使用Lambda表达式而不是Foreach循环。 任何人都可以建议我这样做...。 解决方案 你可以使用LINQ在集合上运行循环,如下所示 - lob.SalesPlanCollection.ForEach(i => i.DoStuff()); 我的第一个问题是为什么?你想把它转换成Lambda吗?因为我个人没有看到代码有什么问题 但要回答你的问题; lob.SalesPLanCollection.ForEach(i = > { / / 比较PlanEffectiveDate等于CurrentRenewalDate。 if (purchasePlanRequest.PlanEffectiveDate = = currentRenewalDate) { if (salepln.IsRenewalPlan == true ) { currentPlan.Add(salepln); } else { if (salepln.IsCurrentPlan == true ) { currentPlan.Add(salepln) ; } } } }); 您可以在表达式中添加Where子句,以限制迭代的数据量... public static Collection< salesplan> GetCurrentPlan(PurchasePlanRequest purchasePlanRequest) { SalesCustomerManager salesCustomerManager = new SalesCustomerManager(); SalesCustomer salesCustomer = new SalesCustomer(); salesCustomer.ReportingCustomerNumber = purchasePlanRequest.ReportingCustomerId; Collection< salesplan> salesPlanCollection = new Collection< salesplan>(); salesCustomer = salesCustomerManager.GetCustomerLinesPlansCoverages(salesCustomer); Collection< salesplan> renewalPlan = new Collection< salesplan>(); Collection< salesplan> currentPlan = new Collection< salesplan>(); foreach(SalesLineOfBusiness lob in salesCustomer.SalesLineOfBusinessCollection.Where(o => o.LineOfBusinessCode == purchasePlanRequest.LOBCode)) { lob.SalesPLanCollection.ForEach(i = > { //比较PlanEffectiveDate等于CurrentRenewalDate。 if(purchasePlanRequest.PlanEffectiveDate == currentRenewalDate) { if(salepln.IsRenewalPlan == true) ) { currentPlan.Add(salepln); } else { if(salepln.IsCurrentPlan == true) { currentPlan.Add(salepln); } } } }); } return currentPlan; }< / salesplan>< / salesplan>< / salesplan>< / salesplan>< / salesplan>< / salesplan>< / salesplan> • public static Collection<SalesPlan> GetCurrentPlan(PurchasePlanRequest purchasePlanRequest) { SalesCustomerManager salesCustomerManager = new SalesCustomerManager(); SalesCustomer salesCustomer = new SalesCustomer(); salesCustomer.ReportingCustomerNumber = purchasePlanRequest.ReportingCustomerId; Collection<SalesPlan> salesPlanCollection = new Collection<SalesPlan>(); salesCustomer = salesCustomerManager.GetCustomerLinesPlansCoverages(salesCustomer); Collection<SalesPlan> renewalPlan = new Collection<SalesPlan>(); Collection<SalesPlan> currentPlan = new Collection<SalesPlan>(); foreach (SalesLineOfBusiness lob in salesCustomer.SalesLineOfBusinessCollection) { if (lob.LineOfBusinessCode == purchasePlanRequest.LOBCode) { foreach (SalesPlan salepln in lob.SalesPlanCollection) { //Compare PlanEffectiveDate is equal to CurrentRenewalDate. if (purchasePlanRequest.PlanEffectiveDate == currentRenewalDate) { if (salepln.IsRenewalPlan == true) { currentPlan.Add(salepln); } } else { if (salepln.IsCurrentPlan == true) { currentPlan.Add(salepln); } } } } } return currentPlan; }In the bold code foreach loop, I want to use Lambda expression instead of Foreach loop.Can anybody suggest me approach of doing so…. 解决方案 You can run a loop over a collection using LINQ as below -lob.SalesPlanCollection.ForEach(i => i.DoStuff());My first question would be WHY? would you want to convert it too Lambda? As I personally dont see anything wrong with the codebut to answer your question;lob.SalesPLanCollection.ForEach(i =>{ //Compare PlanEffectiveDate is equal to CurrentRenewalDate. if (purchasePlanRequest.PlanEffectiveDate == currentRenewalDate) { if (salepln.IsRenewalPlan == true) { currentPlan.Add(salepln); } else { if (salepln.IsCurrentPlan == true) { currentPlan.Add(salepln); } } }});You can add a Where clause to your expression to limit the amount of data being iterated over as well...public static Collection<salesplan> GetCurrentPlan(PurchasePlanRequest purchasePlanRequest) { SalesCustomerManager salesCustomerManager = new SalesCustomerManager(); SalesCustomer salesCustomer = new SalesCustomer(); salesCustomer.ReportingCustomerNumber = purchasePlanRequest.ReportingCustomerId; Collection<salesplan> salesPlanCollection = new Collection<salesplan>(); salesCustomer = salesCustomerManager.GetCustomerLinesPlansCoverages(salesCustomer); Collection<salesplan> renewalPlan = new Collection<salesplan>(); Collection<salesplan> currentPlan = new Collection<salesplan>(); foreach (SalesLineOfBusiness lob in salesCustomer.SalesLineOfBusinessCollection.Where(o => o.LineOfBusinessCode == purchasePlanRequest.LOBCode)) { lob.SalesPLanCollection.ForEach(i => { //Compare PlanEffectiveDate is equal to CurrentRenewalDate. if (purchasePlanRequest.PlanEffectiveDate == currentRenewalDate) { if (salepln.IsRenewalPlan == true) { currentPlan.Add(salepln); } else { if (salepln.IsCurrentPlan == true) { currentPlan.Add(salepln); } } } }); } return currentPlan; }</salesplan></salesplan></salesplan></salesplan></salesplan></salesplan></salesplan> 这篇关于如何将正常的foreach循环转换为c#中的lambda表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-25 07:50