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:
<span style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Person
<span style="color: #606060"> 2:</span> {
<span style="color: #606060"> 3:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Age { get; set; }
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> Leader { get; set; }
<span style="color: #606060"> 6:</span> }
And let's load up some sample data:
<span style="color: #606060"> 1:</span> Person[] people = <span style="color: #0000ff">new</span> Person[] {
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">new</span> Person { Name = <span style="color: #006080">"Blue"</span>, Age = 25, Leader = <span style="color: #0000ff">true</span> },
<span style="color: #606060"> 3:</span> <span style="color: #0000ff">new</span> Person { Name = <span style="color: #006080">"Gold"</span>, Age = 16, Leader = <span style="color: #0000ff">false</span> },
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">new</span> Person { Name = <span style="color: #006080">"Red"</span>, Age = 27, Leader = <span style="color: #0000ff">false</span> },
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">new</span> Person { Name = <span style="color: #006080">"Green"</span>, Age = 14, Leader = <span style="color: #0000ff">false</span>}
<span style="color: #606060"> 6:</span> };
Now what we could do to find the leader (the assumption is that there is always only one leader):
<span style="color: #606060"> 1:</span> Person leader = people.Where(p => p.Leader == <span style="color: #0000ff">true</span>).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":
<span style="color: #606060"> 1:</span> Person leader2 = people.First(p => p.Leader == <span style="color: #0000ff">true</span>);
<span style="color: #606060"> 2:</span> Person leader3 = people.Single(p => p.Leader == <span style="color: #0000ff">true</span>);
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:
<span style="color: #606060"> 1:</span> Person firstChild1 = people.First(p => p.Age < 18);
<span style="color: #606060"> 2:</span> 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.