LINQ can throw an exception on .First() for an empty set

| | Comments (0)
Pretty much what it says in the title!

If you have a resultset from your LINQ operation that contains nothing and you call .First() then you'll get an InvalidOperationException "Sequence contains no elements". Call FirstOrDefault instead, as that returns, if there's nothing well, a default.

Leave a comment

About this Entry

This page contains a single entry by Rob published on February 1, 2009 2:51 PM.

Handmade Crafts Directory was the previous entry in this blog.

Also Reading is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 5.02