Linq对实体 - 任何VS第一VS存在 [英] Linq To Entities - Any VS First VS Exists
问题描述
我正在使用Entity Framework,我需要检查一个名称=xyz的产品是否存在...
我想我可以使用Any()存在()或First()。
哪一种是这种情况的最佳选择?哪一个有最好的表现?
谢谢,
Miguel
任何在数据库级别转换为Exists。首先翻译成Select Top 1 ...在这些之间,Exists将会执行First,因为实际的对象不需要被提取,只有一个布尔结果值。至少你没有询问.Where(x => x.Count()> 0),这需要整个匹配集进行评估和迭代。你可以确定你有一个记录。任何短路请求,可以明显更快。
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 best option for this kind of situation? Which one has the best performance?
Thank You,
Miguel
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.
这篇关于Linq对实体 - 任何VS第一VS存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!