问题
what is the difference between using exists
over contains
var s = new int[] { 1, 2, 3, 4, 5 };
dbset.where(x => s.contains(x.id);
or
var s = new int[] { 1, 2, 3, 4, 5 };
dbset.Where(x => s.Exists(y => x.id));
回答1:
- Exists is a method of
List<T>
, there is no such method on array orIEnumerable<T>
extensions. - Correct syntax of usage of this method is
x => s.Exists(y => y == x.id)
(you should pass predicate, i.e. method which returns boolean) - The difference is -
Contains
supported by Linq to Entities,Exists
is not supported.
来源:https://stackoverflow.com/questions/15481272/linqtoentities-difference-beteen-exists-and-contains