What is the fastest way to determine if a row exists using Linq to SQL?

前端 未结 5 1519

I am not interested in the contents of a row, I just want to know if a row exists. The Name column is a primary key, so there will either be 0 or 1 matching row

5条回答
  •  佛祖请我去吃肉
    2020-12-14 00:06

    The Count() approach may do extra work, as (in TSQL) EXISTS or TOP 1 are often much quicker; the db can optimise "is there at least one row". Personally, I would use the any/predicate overload:

    if (dc.Users.Any(u => u.Name == name)) {...}
    

    Of course, you can compare what each one does by watching the TSQL:

    dc.Log = Console.Out;
    

提交回复
热议问题