Click here to Skip to main content
15,895,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
                    {
                        //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
Comments
Member 8090436 20-Feb-13 6:39am    
Thanks,
But what is in DoStuff() method....
How i do?

You can run a loop over a collection using LINQ as below -
lob.SalesPlanCollection.ForEach(i => i.DoStuff());
 
Share this answer
 
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;

C#
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);
      }
    }
  }

});
 
Share this answer
 
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>
 
Share this answer
 
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)



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