Using the LINQ Keyword "First"

All day I was stumped on why there wasn't a simple way to grab the first object in a sequence other than using:

.Take(1).Single

Then I discovered the keyword First. I'm still a little puzzled as to why .Single would throw an exception if more than a single element would be returned. I can't really see a practical use for it unless you wanted to force only 1 object being returned.

Published Tuesday, July 08, 2008 2:42 PM by Jason N. Gaylord
Filed under: ,

Comments

# re: Using the LINQ Keyword "First"

Tuesday, July 08, 2008 3:30 PM by djsolid

In case you want to update something. Like an order.

# re: Using the LINQ Keyword "First"

Tuesday, July 08, 2008 4:17 PM by mwilson

Returning an exception is appropriate in some cases. If I'm looking up a record in LINQ to SQL by a unique column and the program depends on this column being unique, I'll use Single(). Now, if someone changes the table to allow duplicates in that column, and the duplicate actually happens, we've got coverage.

I might be wrong, but I think Take() is converted to SELECT TOP by LINQ, in which case the fact that there were now multiple records with the same value in a column I had previously assumed to be unique would go undetected. That won't always matter, but when it does, Single() seems like the right way to go.

# re: Using the LINQ Keyword "First"

Tuesday, July 08, 2008 9:47 PM by Chris Szurgot

It doesn't force a single item to return, it means you only expect a single item so it returns a single item instead of a IEnumerable (or IQueryable). That's why it throws and exception when there is more than one, because it can't be coerced into one value.

# re: Using the LINQ Keyword "First"

Tuesday, July 08, 2008 10:03 PM by Hal

Don't pass on .FirstOrDefault either. It allows a single take and returns null if there are no results...

var obj = tbl.FirstOrDefault();

if (obj == null) { obj = new tbl(); } // or throw... whatever

Hal

# re: Using the LINQ Keyword "First"

Wednesday, July 09, 2008 5:36 AM by Petar Petrov

I totally agree with mwilson. Single() has its use. It will help you to find errors related to duplicates.

Hal you are wrong. In fact you are not totally correct with this statement - "and returns null if there are no results". It will not return null if there are no results, it will return the default(T) and for reference types it will be null. However that doesn't forcibly mean there is no results. It's possible that your List<string> for example contains default(string) which is null.

Take a look at this example(for value types):

           var scores = new List<int>();

           var first = (from n in scores

                        select n).FirstOrDefault();

first will be "valid", it will be '0', even in this case where our list doesn't contain any elements. Here I think the situation is worst because zero "is more valid" then null. ;)

So always think twice when there's a method returning default(T).

# re: Using the LINQ Keyword "First"

Tuesday, February 23, 2010 10:50 AM by Justin

I am a little puzzled as to why .Count would return an int, unless you wanted to get the number of items in a sequence.

# re: Using the LINQ Keyword "First"

Monday, March 08, 2010 10:32 AM by Rayb

I am puzzled too why .Where filters a set of objects, unless of course you wanted a filtered set of objects.

# re: Using the LINQ Keyword "First"

Monday, March 08, 2010 10:36 AM by HorneyS

I can't understand why indexing past the bounds of an array throws an exception unless of course the designers wanted people not to index past the  bounds of the array.

# re: Using the LINQ Keyword "First"

Sunday, May 22, 2011 5:56 PM by weblogs.asp.net

Using the linq keyword quot first quot.. Peachy :)

Leave a Comment

(required) 
(required) 
(optional)
(required)