【问题标题】:Entity Framework Query : Include with where实体框架查询:包含在哪里
【发布时间】:2019-02-14 14:59:52
【问题描述】:

我想获取所有图层组并包括(急切加载)具有特定 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 example1 的情况下才能获取组。我想获得具有正确 URL 的图层,即使这些组具有其他 url:s 的图层。

【问题讨论】:

  • 很遗憾,您无法过滤子项。不过,您也许可以自下而上重写查询。选择具有所需 URL 的图层并从那里导航到父对象。

标签: entity-framework linq include eager-loading


【解决方案1】:

According to this 您不能过滤 IncludeThenInclude 集合,但您可以使用基于 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();

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多