SingleOrDefault() throws an exception on more than one element

后端 未结 7 2018
野性不改
野性不改 2021-02-05 12:17

I\'m getting an exception whenever I fetch like this

Feature f = o.Features.SingleOrDefault(e => e.LinkName == PageLink);

because this can r

7条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 12:23

    SingleOrDefault suggests that you are expecting 0 or 1 results from your query. If you have more than 1 then there is something wrong with your data or query.

    If you are expecting more than 1 result and only want the first one, then FirstOrDefault should be used.

提交回复
热议问题