Click here to Skip to main content
12,076,716 members (45,877 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
•   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….
Posted 20-Feb-13 1:24am
Comments
Member 8090436 20-Feb-13 6:39am
   
Thanks,
But what is in DoStuff() method....
How i do?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can run a loop over a collection using LINQ as below -
lob.SalesPlanCollection.ForEach(i => i.DoStuff());
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

My first question would be WHY? would you want to convert it too Lambda? As I personally dont see anything wrong with the code

but 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);
      }
    }
  }
 
});
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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();
 
<pre>
        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 =&gt; o.LineOfBusinessCode == purchasePlanRequest.LOBCode))
        {
 
            lob.SalesPLanCollection.ForEach(i =&gt;
            {
              //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>
  Permalink  
Comments
Matt T Heffron 7-Dec-15 18:58pm
   
This question was two and a half years old.
Resurrecting it with a "solution" of questionable value is considered "poor form"

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 7 Dec 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100