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

Feedback

Posted on 7/5/2010 11:44:44 AM

Thsna for givig the good expamle of lambda expression.

regrdas
satya
www.walkincg.com

Posted on 12/2/2010 1:32:27 PM

Thanks a lot! These are really very good examples.

Posted on 12/2/2010 1:32:52 PM

thanks!

Posted on 3/12/2011 1:18:22 PM

string strVal = SomesTringCollection.First(str => str[0] == 'V');
- I'm confused here :(

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 - 2012 Vikram Lakhotia