给定以下类:
public class Nation
{
public string Name { get; set; }
public IEnumerable<City> Cities { get; private set; }
}
public class City
{
public string Name { get; set; }
}
假设Nation
是聚合根,所以我只有一个 NationRepository
而不是 CityRepository
(因此 Nation
是 Linq 查询的起点)。澄清一下,我的起点是 IQueryable<Nation>
对象。
我将如何编写返回 City
集合的查询?根据以下逻辑对象:
全选City
Name
的实例以 'M' 开头,其父级 Nation
的名字是'UK'?
最佳答案
您将执行以下操作:
var results = NationRepository.Where(n => n.Name == "UK")
.SelectMany(n => n.Cities)
.Where(c => c.Name.StartsWith("M"));
关于c# - Linq - 子对象上的where子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6762828/