13,047,502 members (64,227 online)
Technical Blog
alternative version

#### Stats

20.2K views
3 bookmarked
Posted 1 May 2009

# Returning a Single Element With LINQ First and LINQ Single

, 1 May 2009
 Rate this:
Sometimes you have a list, and you need to return a single element from the list. There are several ways to get the element to return. Below are two ways using LINQ with Lambda expressions.Consider the following class: 1: public class Person 2: { 3: public strin

Sometimes you have a list, and you need to return a single element from the list. There are several ways to get the element to return. Below are two ways using LINQ with Lambda expressions.

Consider the following class:

```1: public class Person
2: {
3:     public string Name { get; set; }
4:     public int Age { get; set; }
5:     public bool Leader { get; set; }
6: }```

And let's load up some sample data:

```1: Person[] people = new Person[] {
2:     new Person { Name = "Blue", Age = 25, Leader = true },
3:     new Person { Name = "Gold", Age = 16, Leader = false },
4:     new Person { Name = "Red", Age = 27, Leader = false },
5:     new Person { Name = "Green", Age = 14, Leader = false}
6: };```

Now what we could do to find the leader (the assumption is that there is always only one leader):

`1: Person leader = people.Where(p => p.Leader == true).ToArray()[0];`

The result of the people.Where() is an IEnumerable<Person>.  And you can't just index the first element of that – so you convert it to an array and index that instead.

LINQ provides two methods to perform this type of query without the need of having an intermediate array -- "First" and "Single":

```1: Person leader2 = people.First(p => p.Leader == true);

The difference between the two is that First grabs the first item it finds.  The Single method expects only a single matching item and will throw an exception if it finds more than one.  In this case, there is only one Person in the array that has Leader set to true so both of these lines of code produce the same result.

However, in the situation below:

```1: Person firstChild1 = people.First(p => p.Age < 18);
2: Person firstChild2 = people.Single(p => p.Age < 18);```

The first line will succeed.  The second line will fail since there are two people that are under 18.

A list of licenses authors might use can be found here

## Share

 Software Developer (Senior) United States
Winner - Best Mobile App - AT&T Developer Summit, Las Vegas, 2013

My personal resume can be found at: http://www.philippiercedeveloper.com

My game portfolio can be found at: http://www.rocketgamesmobile.com

I am a software developer with twenty years experience in game development, mobile, web, desktop, server, and database. My extensive background highlights an expertise in rapid application development using the latest Microsoft, Mobile, and Game Development technologies, along with the ability to create AI for games and business software, redesign existing software, develop multi-threaded software, and create client/server applications.

## You may also be interested in...

 Pro

 First Prev Next
 My vote of 1 thund3rstruck1-May-09 4:29 thund3rstruck 1-May-09 4:29
 My vote of 1 Josh Fischer1-May-09 4:19 Josh Fischer 1-May-09 4:19
 My vote of 1 Dave Kreskowiak1-May-09 3:23 Dave Kreskowiak 1-May-09 3:23
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jul-17 1:47 Refresh 1