What is the difference between Contains and Any in LINQ?

前端 未结 5 1556
孤独总比滥情好
孤独总比滥情好 2020-11-30 08:49

What is the difference between Contains and Any in LINQ?

5条回答
  •  半阙折子戏
    2020-11-30 08:59

    Contains checks if the sequence contains a specified element.

    Enumerable.Any checks if element of a sequence satisfies a condition.

    Consider the following example:

    List list = new List { 1, 2, 3, 4, 5 };
    bool contains = list.Contains(1); //true
    
    bool condition = list.Any(r => r > 2 && r < 5);
    

提交回复
热议问题