linq到ienumerable儿童无数

时间:2012-03-27 10:32:55

标签: c# linq ienumerable

我怎样才能选择id为1的停车场,只选择该公园内200年的车辆

var List<Parking> cityParkings = new List<Parking>
{
    new Parking{ id = 1, carsInPark = new List<Car>{ new Car{ year = 2000}, new Car{ year = 2001} }},
    new Parking{ id = 2, carsInPark = new List<Car>{ new Car{ year = 2000}, new Car{ year = 1999} }},
    new Parking{ id = 3, carsInPark = new List<Car>{ new Car{ year = 2005}, new Car{ year = 2000} }},
}

预期结果:

Parking { id = 1, carsInPark = List<Car>{ Car{ year = 2000} } }

选择现有汽车实例的id 1的现有停车实例,但仅选择2000年的汽车。

谢谢;)

1 个答案:

答案 0 :(得分:1)

尝试

cityParkings.Where(p => p.id = 1)
   .Select(pp => new Parking(){id = pp.id, carsInPark = pp.carsInPark.Where(c=>c.year == 2000).ToList()});