Some more Lambda expression

Hi,

Continuing with my work on different Lambda expression. Here are some more Lambda’s that I wrote (when I was practicing them).

You can get my previous example here
http://www.vikramlakhotia.com/Starting_with_the_basic_of_Lambda_Expression.aspx
http://www.vikramlakhotia.com/Some_examples_on_how_to_use_Lambda_Expression.aspx
http://www.vikramlakhotia.com/Using_Lambda_expressions_in_LINQ.aspx

To get the first record matching certain condition in the list

string strVal = SomesTringCollection.First(str => str[0] == 'V');

As you would have guessed to get the last record we have a Last method

string strVal = SomesTringCollection.Last(str => str[0] == 'V');

Another of very important usage of  Lambda can be to create a new anoynomoust type. We can use the select method to create a new anoynomoys types in a Lambda expression. Here is an example.

var NewAnonymousType = strVal.Select(s => new {FirstName = s.Substring(0,10), LastName = s.Substring(10)});

Lambda expression can be used in the grouping also. Below is an example where by I am using a Lambda expression to group Number in Odd and Even;
 
int[] iList = new []{1,2,3,4,5,6,7,8,9};

iList.GroupBy(i => i%2 == 0);

Vikram

Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia