LINQ extension methods - Any() vs. Where() vs. Exists()

后端 未结 7 1696
清酒与你
清酒与你 2020-12-02 19:51

Unfortunately the names of these methods make terrible search terms, and I\'ve been unable to find a good resource that explains the difference between these methods--as in

7条回答
  •  醉梦人生
    2020-12-02 20:17

    Where returns a new sequence of items matching the predicate.

    Any returns a Boolean value; there's a version with a predicate (in which case it returns whether or not any items match) and a version without (in which case it returns whether the query-so-far contains any items).

    I'm not sure about Exists - it's not a LINQ standard query operator. If there's a version for the Entity Framework, perhaps it checks for existence based on a key - a sort of specialized form of Any? (There's an Exists method in List which is similar to Any(predicate) but that predates LINQ.)

提交回复
热议问题