LINQ Single() Exception for 0 or multiple items

前端 未结 2 648
抹茶落季
抹茶落季 2020-12-31 00:16

I have some IEnumberable collection of items. I use .Single() to find a specific object in the collection.

I choose to use Single()

2条回答
  •  隐瞒了意图╮
    2020-12-31 00:50

    What you want is SingleOrDefault()

    The "or default" actually means it returns null (for reference types) or whatever the default would be for a non-reference type. You'll need to new-up an object to take its place.

提交回复
热议问题