Linq To Entities - Any VS First VS Exists

后端 未结 5 1147
滥情空心
滥情空心 2021-01-07 21:36

I am using Entity Framework and I need to check if a product with name = \"xyz\" exists ...

I think I can use Any(), Exists() or First().

Which one is the b

5条回答
  •  抹茶落季
    2021-01-07 22:03

    Any translates into "Exists" at the database level. First translates into Select Top 1 ... Between these, Exists will out perform First because the actual object doesn't need to be fetched, only a Boolean result value.

    At least you didn't ask about .Where(x => x.Count() > 0) which requires the entire match set to be evaluated and iterated over before you can determine that you have one record. Any short-circuits the request and can be significantly faster.

提交回复
热议问题