Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

October 11, 2009

Lambda Expressions: Finding an Item in a Generic List

Filed under: C#,Lambda Expressions,LINQ,VB.NET @ 2:52 pm

In this prior post, I detailed how to build lists of things using a generic List<T>. Once you have a list, you may need to find items in the list. There are several ways to accomplish this, but using lambda expressions is the most concise.

[To begin with an overview of lambda expressions, start here.]

For example, here is how you find an item in a generic list using a for/each statement.

In C#:

Customer foundCustomer = null;
foreach (var c in custList)
{
    if (c.CustomerId == 4)
    {
        foundCustomer = c;
        break;
    }
}
Debug.WriteLine(foundCustomer);

In VB:

Dim foundCustomer As Customer = Nothing
For Each c As Customer In custList
    If c.CustomerId = 4 Then
        foundCustomer = c
        Exit For
    End If
Next
Debug.WriteLine(foundCustomer)

Notice the amount of code required to loop through each item in the list and find the one with a particular customer Id.

You may have heard about language integrated query (LINQ) and how you can use it to search a  list.

In C#:

Customer foundCustomer = null;
var query = from c in custList
            where c.CustomerId == 4
            select c;
foundCustomer = query.FirstOrDefault();
Debug.WriteLine(foundCustomer);

In VB:

Dim foundCustomer As Customer = Nothing
Dim query = From c As Customer In custList _
            Where c.CustomerId = 4 _
            Select c
foundCustomer = query.FirstOrDefault()
Debug.WriteLine(foundCustomer)

So LINQ is cool, but the code does not seem much shorter.

Now let’s look at using lambda expressions to find the item in the list.

In C#:

Customer foundCustomer = null;
foundCustomer = custList.FirstOrDefault(c =>
                        c.CustomerId == 4);
Debug.WriteLine(foundCustomer);

The lambda expression syntax in C# looks like this:

c => c.CustomerId == 4

The code begins with the set of parameters to the lambda expression. The => is the “goes to” or lambda operator. The remainder of the code is the expression itself. In this case, checking for the item in the list where CustomerId is 4.

In VB:

Dim foundCustomer As Customer = Nothing
foundCustomer = custList.FirstOrDefault(Function(c)
                        c.CustomerId = 4)
Debug.WriteLine(foundCustomer)

The lambda expression syntax in VB looks like this:

Function(c) c.CustomerId = 4

The code begins with the word “Function” along with the set of parameters to the lambda expression. The remainder of the code is the expression itself. In this case, checking the item in the list where CustomerId is 4.

.NET 3.5 added a large list of extension methods on the Enumerable class. Any object that implements IEnumerable or IEnumerable<T> (basically any object you can do a for/each over) can use these methods. Many of these extension methods (such as the FirstOrDefault method  shown in the above example) support lambda expressions.

The FirstOrDefault extension method of the Enumerable class returns the first item in the list or the default value for the object. If you pass it a lambda expression, it returns the first item in the list that matches the Boolean expression.

Enjoy!

2 Comments

  1.   Alexandre lebuenovox@hotmail.com — January 6, 2010 @ 4:03 pm    Reply

    Thanks Deborah, your article helped me a lot.

    Alexandre from Brazil

  2.   Rosen — December 7, 2012 @ 10:41 am    Reply

    Thanks Deborah,
    This is very good example.
    It is simple and tidy.
    Good luck!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs