Useful Lambda Expressions

A really useful built in feature of .Net is the Lambda expression. I've been using these for a while, but until I started looking into it, I never realized how useful these can actually be. There are loads of quick, nifty ways to get things done that might take twice as long if not using a lambda expression.

All lambda expressions use the lambda operator =>, which is read as "goes to". Sometimes these expressions look a little confusing, but once you understand the general use they are quite easy to read.


Lets take a look at how we traditionally found an item in a generic list:

List<Car> carColours = new List<Car>();

foreach (Car car in carColours)


    if (car.Colour  == "red")


        return car.Colour;



The above code works, but lets try and make this sexy with a Lambda expression:

List<Car> carColours = new List<Car>();

carColours.FirstOrDefault(car => car.Colour == "Red");

So, let me explain the code above. Im using the lambda expression to search the list for the first value that has a colour of Red. In one line, I can now do what previously took me four. I also find that this is a lot easier to read. Here are some other useful expressions to use :-

1. Populate a new list with values that match

<Car> matches = carColours.FindAll(val => val.Colour != "Red");

2. Another example of getting values from a list

var weightLessThanSix = carColours.TakeWhile(n => n.Weight < 6);

We could supply multiple parameters and return all numbers until a number is encountered whose value is less than its position.

matches = carColours.TakeWhile((n, index) => n.Weight >= index);

3. Say for example I wanted to provide a cleaner way to declare a event handler.

This was the traditional way:

public void CreateControls()


    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);



protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)


    // Do something


Which now becomes -

void CreateControlsLambda()


    DropDownList1.SelectedIndexChanged += (sender, e) => { Button1.Text = "Event Fired"; };


Much slicker!

4. Find the lowest or highest value in a list

decimal maxWeight = cars.Max(car => car.Weight);

decimal minWeight = cars.Min(car => car.Weight);


Or the weight between a range

decimal weightBetween = cars.FirstOrDefault(car => car.Weight < 9.0m).Weight;

As you can see, using lambda expressions can make your code a lot slicker and more readable. Some of the examples I have given are only really the tip of the iceberg when it comes to working with lambda expressions. If you have any examples that could be useful or any suggestions, please let me know and I'll add it to this list.

For some further reading check out:


DevX - 3/24/2010
Nice man...Took me a while to get warmed up to lambda's but I'm starting to come around!!

Nishit - 4/28/2011
Good work... Nice and clean way to explane. Looking farword to see some more on Lambda expression

sqrtpi - 11/27/2011
You should also mention that half the credit belongs to LINQ, it's really made a home for lambda expressions.

Brian Noronha - 1/20/2012
A really good starters article which highlights the cream of Lambda expressions.

Lambday - 11/2/2014
This is it.

Ben - 1/19/2015
You've got a small error in your first example: return car.Colour; //returns the colour carColours.FirstOrDefault(car => car.Colour == "Red"); //returns the carobject colour of an object and the object itself are not the same!

Add your comment

300 Characters left

Please fill this in to confirm that you are human