C# – Linq code to select one item

clinq

I find myself writing a lot of code like this to select one item that matches

var item = (from x in Items where x.Id == 123 select x).First();

Is there a cleaner way of doing it or is this as concise as I'm going to get?

EDIT: Should have said "Cleaner way using linq syntax". I was already aware of the lambda syntax and it's starting to look like this is actually the only way. I did get some useful info though, so thanks to everyone who replied.

Best Answer

Depends how much you like the linq query syntax, you can use the extension methods directly like:

var item = Items.First(i => i.Id == 123);

And if you don't want to throw an error if the list is empty, use FirstOrDefault which returns the default value for the element type (null for reference types):

var item = Items.FirstOrDefault(i => i.Id == 123);

if (item != null)
{
    // found it
}

Single() and SingleOrDefault() can also be used, but if you are reading from a database or something that already guarantees uniqueness I wouldn't bother as it has to scan the list to see if there's any duplicates and throws. First() and FirstOrDefault() stop on the first match, so they are more efficient.

Of the First() and Single() family, here's where they throw:

  • First() - throws if empty/not found, does not throw if duplicate
  • FirstOrDefault() - returns default if empty/not found, does not throw if duplicate
  • Single() - throws if empty/not found, throws if duplicate exists
  • SingleOrDefault() - returns default if empty/not found, throws if duplicate exists