我的列表如下:
number_list = (1, 2, 3, 4, 5, 6, 7).
我想获取包含大于3的元素的列表。应该是这样的。
new_list = (4, 5, 6, 7)
我可以执行诸如foreach之类的操作,以检查每个元素,直到小于3。但是还有其他方法吗?还是某些List方法可以做到这一点?
答案 0 :(得分:1)
答案 1 :(得分:0)
一种方法是FindAll
,它返回一个List
List<int> items = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
items = items.FindAll(x => x < 3);
另一种方法是将Where
与ToList
组合
List<int> items = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
items = items.Where(x => x < 3).ToList();