开发者

LINQ - Sequence contains no elements

I am using a LINQ query as below.

object.collection.where(t => t.id.Equals("2")).First();

I am getting th开发者_开发知识库e error "Sequence contains no elements". Why does the result throw an error when the result contains no elements? Should it not return null when no results are found? That is what happens when using SQL.


It's working as designed. The First() method is to be called when it's known at least one row will be returned. When this isn't the case, call FirstOrDefault().


object.collection.where(t => t.id.Equals("2")).FirstOrDefault();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜