Some more Lambda expression
Posted on 9/24/2007 10:33:00 AM
in #Asp.Net 3.X
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
|
Posted on 7/5/2010 11:44:44 AM
Thsna for givig the good expamle of lambda expression.
regrdas
satya
www.walkincg.com
|