我有如下实体关系:
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”中为每个设备包括“ Designer”,并返回此列表。我正在尝试以下方法:
Incident tmp = _context.Incidents
.Where(x => x.Id == incid)
.Include(x => x.EquipmentAssignments)
.ThenInclude(x => x.Select(s => s.Designator)).First();
但我收到以下错误:
附加信息:属性表达式'x => {from x中的Equipment s select [s] .Designator}'无效。该表达式应表示属性访问:“ t => t.MyProperty”。指定多个属性时,请使用匿名类型:'t => new {t.MyProperty1,t.MyProperty2}'。
我尝试使用匿名类型.ThenInclude(x => x.Select(s => new { s.Designator})
无济于事,并且不确定如何完成我所需要的。感谢您的任何帮助。
您不能在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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句