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);
需要显示 item3 < 0 的计数元组
我的变体(不正确):
var r roads.Select(t => t.Item3 < 0).Count;
最佳答案
Linq 提供了 Count
的重载采用 Func<T, bool>
的方法并将计算满足该条件的元素的数量:
int countLessThanZero = roads.Count(t => t.Item3 < 0);
或者,您可以先过滤列表,然后计算过滤列表中的元素数:
int countLessThanZero = roads.Where(t => t.Item3 < 0).Count();
关于c# - 从列表中返回元组计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175190/