Entity Framework 查询:包含在何处
Entity Framework Query : Include with where
我想获取所有图层组并包括(预先加载)具有特定 url 的图层。
这是我的表格:
- LayerGroup (id, List<Layer>layers and more...)
- Layer(id, url, List<Attribute>attributes and more...)
- Attribute (id and more...)
这就是我到目前为止所得到的。
var groups = _context.LayerGroups
.Where(group => group.IsActive)
.Where(g => g.Layers.All(l => l.IsActive == true))
.Where(g => g.Layers.All(l => l.Url == "example1"))
.Include(g => g.Layers)
.ThenInclude(layer => layer.Attributes)
.Include(group => group.Layers).ThenInclude(layer => layer.SearchEngines)
.ToList();
但它似乎只有在所有图层都处于活动状态并且具有 url 示例 1 时才会获得组。我想获得正确 Url 的图层,即使这些组具有其他 url:s.
的图层
According to this 您无法过滤 Include
和 ThenInclude
集合,但您可以使用基于 Include
的条件过滤父集合。如果需要,您可以按如下方式编写查询:
var groups = _context.LayerGroups.Where(group => group.IsActive)
.Include(g => g.Layers)
.ThenInclude(layer => layer.Attributes)
.Include(group => group.Layers)
.ThenInclude(layer => layer.SearchEngines)
.Where(g => g.Layers.Any(l => l.IsActive == true && l => l.Url == "example1"))
.ToList();
我想获取所有图层组并包括(预先加载)具有特定 url 的图层。
这是我的表格:
- LayerGroup (id, List<Layer>layers and more...)
- Layer(id, url, List<Attribute>attributes and more...)
- Attribute (id and more...)
这就是我到目前为止所得到的。
var groups = _context.LayerGroups
.Where(group => group.IsActive)
.Where(g => g.Layers.All(l => l.IsActive == true))
.Where(g => g.Layers.All(l => l.Url == "example1"))
.Include(g => g.Layers)
.ThenInclude(layer => layer.Attributes)
.Include(group => group.Layers).ThenInclude(layer => layer.SearchEngines)
.ToList();
但它似乎只有在所有图层都处于活动状态并且具有 url 示例 1 时才会获得组。我想获得正确 Url 的图层,即使这些组具有其他 url:s.
的图层According to this 您无法过滤 Include
和 ThenInclude
集合,但您可以使用基于 Include
的条件过滤父集合。如果需要,您可以按如下方式编写查询:
var groups = _context.LayerGroups.Where(group => group.IsActive)
.Include(g => g.Layers)
.ThenInclude(layer => layer.Attributes)
.Include(group => group.Layers)
.ThenInclude(layer => layer.SearchEngines)
.Where(g => g.Layers.Any(l => l.IsActive == true && l => l.Url == "example1"))
.ToList();