使用Linq确定数据库中是否存在记录的最佳方法

时间:2019-07-02 12:06:43

标签: c# entity-framework linq

我想使用Linq确定数据库中是否存在记录

我进行了性能测试,以确定哪种方法更快,并且看起来很等效。

选项1:

MyClass my_instance = (from q in data_context.MyClass where q.id == "my_id" select q).FirstOrDefault();
bool is_exists = (my_instance != null); 

选项2:

bool is_exists = data_context.MyClass.Any(q => q.id == "my_id");

有没有一种方法比另一种更好?有没有其他更快的方法?

0 个答案:

没有答案
相关问题