EF7在列表

时间:2016-07-05 03:55:50

标签: entity-framework entity-framework-core

我有如下实体关系:

public class Incident
{
    [Key]
    public int Id { get; set; } 

    public List<Equipment> EquipmentAssignments { get; set; }

}

public class Equipment
{
    public int EquipmentId { get; set; }         
    public Incident Incident { get; set; }
    public Designator Designator { get; set; }//I WANT TO INCLUDE THIS
}

我正在尝试为“EquipmentAssignments”中的每个设备添加“指定符”并返回此列表。我正在尝试以下方法:

 Incident tmp = _context.Incidents
                        .Where(x => x.Id == incid)
                        .Include(x => x.EquipmentAssignments)
                        .ThenInclude(x => x.Select(s => s.Designator)).First();

但是我收到以下错误:

  

附加信息:属性表达式'x =&gt; {从   x select [s] .Designator}'中的设备无效。表达方式   应代表属性访问:'t =&gt; t.MyProperty”。什么时候   指定多个属性使用匿名类型:'t =&gt;新{   t.MyProperty1,t.MyProperty2}'。

我尝试过使用匿名类型.ThenInclude(x => x.Select(s => new { s.Designator})无效,不知道如何完成我需要的工作。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您无法在ThenInclude中传递选择。这是ThenInclude语法:

public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;

只需删除选择,一切都会正常工作!

  var tmp = myConext.Incidents
          .Where(x => x.Id == 1)
          .Include(x => x.EquipmentAssignments)
          .ThenInclude(x => x.Designator).First();