linq to ienumerable where child ienumerable

本文关键字:ienumerable child where linq to | 更新日期: 2023-09-27 18:29:04

我如何选择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年的汽车。

谢谢;)

linq to ienumerable where child ienumerable

尝试

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