【C# / LINQ】Any、All、Contains

【LINQ】Any、All、Contains

使用在陣列內的判斷式範例

【Any】

var list = new List<string>() {"ASUS","Acer","BenQ", "Toshiba","IBM","HP","Dell"};

Console.WriteLine(list.Any (l => l == "Acer"));

Console.WriteLine(list.Any (l => l.Length > 10));

/* 輸出: True

False */

【All】

var list = new List<string>() {"ASUS","Acer","BenQ", "Toshiba", "Dell"};

Console.WriteLine(list.All(l => l.Length > 3));

Console.WriteLine(list.All(l => l.Contains("A")));

/* 輸出: True

False */

【Contains】

var list = new List<string>() {"ASUS","Acer","BenQ", "Toshiba", "Dell"};

Console.WriteLine(list.Contains("Acer"));

Console.WriteLine(list.Contains("A"));

/* 輸出: True

False */

 

文章來源:

http://ithelp.ithome.com.tw/articles/10105069