简体   繁体   English

使用 LINQ 查询四个嵌套实体。 - 包括 Where 条件

[英]Using LINQ to query four nested entities. - Include Where condition

I have four classes Offer, Section, Field, and Option:我有四个类 Offer、Section、Field 和 Option:

Where the offer has sections and every section has some fields and each field has some options as shown:报价有部分,每个部分都有一些字段,每个字段都有一些选项,如下所示:

    public class Offer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Section> Sections { get; set; }
    }

    public class Section
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Field> Fields { get; set; }
    }

    public class Field
    {
        public int Id { get; set; }
        public string Type { get; set; } //[question, group]
        public ICollection<Option> Options { get; set; } 
    }

    public class Option
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

I tried to get the offer by id including the nested entities and this code works perfectly:我试图通过 id 获取报价,包括嵌套实体,这段代码完美运行:

    var offer = _context.Offers
        .Include(o => o.Sections
            .Select(s => s.Fields
            .Select(f => f.Options)))
        .FirstOrDefault(o => o.Id == offerId);

The problem is when I try to filter the Fields by 'type' like this:问题是当我尝试按“类型”过滤字段时,如下所示:

    var offer = _context.Offers
        .Include(o => o.Sections
            .Select(s => s.Fields.Where(f => f.Type == "question")
            .Select(f => f.Options)))
        .FirstOrDefault(o => o.Id == offerId);

and I get this error:我收到这个错误:

The Include path expression must refer to a navigation property defined on the type. Include 路径表达式必须引用类型上定义的导航属性。 Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.对参考导航属性使用虚线路径,对集合导航属性使用 Select 运算符。 Parameter name: path参数名称:路径

I've reviewed lots of questions and still cannot achieve that :(我已经审查了很多问题,但仍然无法做到这一点:(

Linq: query with three nested levels Linq:具有三个嵌套级别的查询

EF LINQ include nested entities [duplicate] EF LINQ 包括嵌套实体 [重复]

Using LINQ to query three entitites. 使用 LINQ 查询三个实体。 - Include path expression must refer to a navigation property defined on the type - 包含路径表达式必须引用类型上定义的导航属性

Include() is used for Eager loading. Include() 用于 Eager 加载。 It is the process whereby a query for one type of entity also loads related entities as part of the query, so that we don't need to execute a separate query for related entities.在这个过程中,对一种类型的实体的查询也会加载相关实体作为查询的一部分,这样我们就不需要对相关实体执行单独的查询。 Where() is currently not supported inside Include. Include 中目前不支持 Where()。

If you want to just filter the result you can do something like this :如果您只想过滤结果,您可以执行以下操作:

        var offer = _context.Offers
         .Include(o => o.Sections
         .Select(s => s.Fields
         .Select(f => f.Options)))
         .FirstOrDefault(o => o.Id == offerId);
        var filtered_offer = 
        new Offer
        {
            Sections = offer.Sections.Select(S => new Section
            {
                Id = S.Id,
                Name = S.Name,
                Fields = S.Fields.Where(f => f.Type == "question").ToList()
            }).ToList(),
            Id = offer.Id,
            Name = offer.Name
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM