Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting a statement using LINQ(C#)

Tags:

c#

linq

Is it possible to write the folowing using lambda(C#)

private static void GetRecordList(List<CustomerInfo> lstCustinfo)
{
    for (int i = 1; i <= 5; i++)
    {
        if (i % 2 == 0)

            lstCustinfo.Add(new CustomerInfo { CountryCode = "USA", CustomerAddress = "US Address" + i.ToString(), CustomerName = "US Customer Name" + i.ToString(), ForeignAmount = i * 50 });

        else
           lstCustinfo.Add(new CustomerInfo { CountryCode = "UK", CustomerAddress = "UK Address" + i.ToString(), CustomerName = "UK Customer Name" + i.ToString(), ForeignAmount = i * 80 });

    }
}
like image 738
Thinking Avatar asked Apr 13 '10 07:04

Thinking


2 Answers

List<CustomerInfo> lstCustinfo = 
    Enumerable.Range(1, 5).Select(i => (i % 2 == 0)
        ? new CustomerInfo { CountryCode = "USA", CustomerAddress = "US Address" + i.ToString(), CustomerName = "US Customer Name" + i.ToString(), ForeignAmount = i * 50 }
        : new CustomerInfo { CountryCode = "UK", CustomerAddress = "UK Address" + i.ToString(), CustomerName = "UK Customer Name" + i.ToString(), ForeignAmount = i * 80 })
    .ToList();
like image 62
Darin Dimitrov Avatar answered Oct 04 '22 23:10

Darin Dimitrov


You must first define the selection, then add it to the incoming list:

var range = Enumerable.Range(1, 5);
var customers = from i in range
                select (i % 2 == 0) ?
                      new CustomerInfo 
                      { 
                                    CountryCode = "USA", 
                                    CustomerAddress = "US Address" + i, 
                                    CustomerName = "US Customer Name" + i, 
                                    ForeignAmount = i * 50 
                      }
                      :
                      new CustomerInfo 
                      { 
                                    CountryCode = "UK", 
                                    CustomerAddress = "UK Address" + i,
                                    CustomerName = "UK Customer Name" + i,
                                    ForeignAmount = i * 80 
                      };

lstCustinfo.AddRange(customers);

Or perhaps even more explicit:

var range = Enumerable.Range(1, 5);
var usCustomers = from i in range
                  where i % 2 == 0 
                select new CustomerInfo 
                      { 
                                    CountryCode = "USA", 
                                    CustomerAddress = "US Address" + i, 
                                    CustomerName = "US Customer Name" + i, 
                                    ForeignAmount = i * 50 
                      };

var ukCustomers = from i in range
                where i % 2 != 0
                select new CustomerInfo 
                      { 
                                    CountryCode = "UK", 
                                    CustomerAddress = "UK Address" + i,
                                    CustomerName = "UK Customer Name" + i,
                                    ForeignAmount = i * 80 
                      };

lstCustinfo.AddRange(usCustomers.Union(ukCustomers));
like image 21
Peter Lillevold Avatar answered Oct 04 '22 22:10

Peter Lillevold