How do you read lambda expressions?

I would read this as:

Select each item individually from myList into a variable s, using s, apply a Trim() on it and once done with all items in myList, convert the whole thing to a list.


Given that LINQ generally only works with IEnumerable objects, you could read s => as 'for each s in the IEnumerable'.

Update: Revisiting this answer over 5 years on, I am deeply unsatisfied with it. Personally, nowadays I find myself considering it as "maps to" or I've seen "such that" which is also pertinent depending on the circumstance.


The first few times, you'll need to break the full expression to bits and convert them to syntax you are familiar with and eventually you'll get familiar with lambda.

In this snippet,

var foo = myList.Select(s => s.Trim()).ToList();

Select() does projection operation similar to that in sql.

s => s.Trim() can be converted to

string SomeMethod(string input)
{
    return input.Trim();
}

and the last ToList() is an extension method that converts IEnumerable<T> to List<T>.

Tags:

C#

.Net

Lambda