tags:

views:

118

answers:

2

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

    }
}
+7  A: 
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();
Darin Dimitrov
Yup! People should know about Enumerable.Range..it opens a lot of doors..
puffpio
This obviously works and answers the question, but it seems *a lot* more readable in the original version!
Graham Clark
Now, the question is: Is this lambda expression really better? When counting lines to write for the developer: yes. When considering the readability for other developers which might have to modify the code later on: absolutely NO! That's why I do not use lambda expressions very often.
gehho
great and thnks for the help.. also I learned something new. thanks again
Thinking
Neat! Just learned about static Enumerable.
Luis Filipe
Nitpicking but: `lstCustinfo` is already defined as a parameter.
Peter Lillevold
+1  A: 

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));
Peter Lillevold