02 December 2007
LINQ: Sequence contains no elements. InvalidOperationException when calling Single
If you call Single to get an object from your DB and the object doesn't exist you will get an InvalidOperationException.
return
this.DataContext.MemberDaos.Single(m => m.MemberID == id);
Instead of Single, use SingleOrDefault, which will return null if the object doesn't exist.
return
this.DataContext.MemberDaos.SingleOrDefault(m => m.MemberID == id);
Hope this helps.
Jon
Comment Notification
If you would like to receive an email when updates are made to this post, please register here
Subscribe to this post's comments using
Comments
Leave a Comment
Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.