LINQ Single() Exception for 0 or multiple items

前端 未结 2 663
抹茶落季
抹茶落季 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:44

    I wouldn't recommend using the try/catch in this scenario, because using exceptions to make logical decisions is resource expensive.

    I would recommend using SingleOrDefault(), and check if the result is null. If it is. Do your creation.

提交回复
热议问题