Click here to Skip to main content
16,004,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In below are my code-
XML
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;
        }


Instead of foreach loop, I want to use Lambda expression.Please suggest some approaches!!!!
Posted
Updated 19-Feb-13 1:06am
v2

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.

[EDIT]
E.g.
C#
...
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]

Cheers
Andi
 
Share this answer
 
v3
Hi,

try below code.
C#
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.
 
Share this answer
 
v2
Comments
Andreas Gieriet 19-Feb-13 7:48am    
I think your first Select(...) should be a SelectMany(...) call since that selection results in a collection again from which you filter.
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900