获取范围内的特定对象字段

时间:2017-02-22 10:09:31

标签: c# linq

我有一个列表复杂类型,让我们说

public class Point {
    public double X;
    public int    Id;
}

List<Point> points = (...);

如何使用LINQ获取给定范围内所有List<int>的{​​{1}},例如Id

我可以使用(minIndex, maxIndex)获取所有点对象,但是如何才能获得points.getRange(minIndex, maxIndex - maxIndex)字段的列表?

3 个答案:

答案 0 :(得分:3)

如果您只想要ID,可以使用Select语句,如下所示:

var ids = points.getRange(minIndex, maxIndex - maxIndex)
                .Select(x => x.Id)
                .ToList();

答案 1 :(得分:1)

简单:

var ids = points.Where(x => x.Id >= minIndex && xId <= maxIndex)
    .Select(x => x.Id)
    .ToList();

答案 2 :(得分:0)

var ids = points.Where(p => (p.Id >= minIndex) &&
                      (p.Id <= maxIndex))
                .Select(p => p.Id)
                .ToList();