List<Tuple<string, string, int>> roads
roads.Add(new Tuple<string, string, int>)("first", "second", 1);
roads.Add(new Tuple<string, string, int>)("first", "second", 2);
roads.Add(new Tuple<string, string, int>)("first", "second", -5);
roads.Add(new Tuple<string, string, int>)("first", "second", 3);
需要show count元组,其中item3&lt; 0
我的变种(不正确):
var r roads.Select(t => t.Item3 < 0).Count;
答案 0 :(得分:5)
Linq提供Count
方法的重载,它接受Func<T, bool>
并计算符合该条件的元素数量:
int countLessThanZero = roads.Count(t => t.Item3 < 0);
或者,您可以先筛选列表,然后计算筛选列表中元素的数量:
int countLessThanZero = roads.Where(t => t.Item3 < 0).Count();