LINQ的Any和All
class Data {
    public int A { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return "A:"+A+",Name:"+Name;
    }
}

List<Data> datas = new List<Data>() {
    new Data() { A = 1,Name = "me"},
    new Data(){ A = 10,Name = "hello"},
    new Data(){ A = 5,Name = "world"}
};

Console.WriteLine(datas.Any(m => m.A == 1));
Console.WriteLine(datas.All(m => m.A > 0));

最后面两句返回的都是true

因为Any方法只要集合中有一条满足条件就返回True

All方法需要集合所有元素都满足条件就返回True


首页 我的博客
粤ICP备17103704号