如何使用c#.Net将正常的foreach循环转换为Lambda表达式? [英] How to Convert normal foreach loop into Lambda expression using c#.Net ?

查看:563
本文介绍了如何使用c#.Net将正常的foreach循环转换为Lambda表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的代码 -

Hi,
In below are my code-

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)
                    {
                        if (salepln.IsCurrentPlan == true)
                        {
                            currentPlan.Add(salepln);
                        }
                    }
                }
            }
            return currentPlan;
        }





而不是foreach循环,我想使用Lambda表达式。请建议一些方法!!!!



Instead of foreach loop, I want to use Lambda expression.Please suggest some approaches!!!!

推荐答案

1)方法是定义一个导致一系列项目的查询

2)然后你将处理项目序列(例如,创建这些项目的集合,而不是单独添加它们)

3)每个 foreach ...如果转换为 from ... where ... select (或 myCollection.Where(...)

4)嵌套的foreach是链接的,或者你使用 SelectMany()



在Google上搜索<$ c $的示例c> SelectMany 。





Eg
1) the approach is to define a query that results in a sequence of items
2) you will then process the sequence of items (e.g. create the collection of these items instead of adding them individually)
3) each foreach...if translates into from... where... select (or myCollection.Where(...))
4) nested foreach is either chained or you use SelectMany()

Search google for an example on SelectMany.


E.g.
...
var query = salesCustomer.SalesLineOfBusinessCollection
            .Where(lob=>lob.LineOfBusinessCode == purchasePlanRequest.LOBCode)
            .SelectMany(lob=>lob.SalesPlanCollection)
            .Where(salepln=>salepln.IsCurrentPlan);
var currentPlan = new Collection<SalesPlan>(query);
...



[/ EDIT]



干杯

Andi


[/EDIT]

Cheers
Andi






尝试下面的代码。

Hi,

try below code.
Collection<salesplan> currentPlan = 
salesCustomer.SalesLineOfBusinessCollection.Where(c => c.LineOfBusinessCode == purchasePlanRequest.LOBCode).Select(s => s.SalesPlanCollection).Where(c => c.IsCurrentPlan == true).ToList<salesplan>();





希望它有所帮助。



hope it helps.


这篇关于如何使用c#.Net将正常的foreach循环转换为Lambda表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆