我有三个模型类
public class Item1{
public int Id;
public List<Item2> Item2List { get; set; }
}
public class Item2{
public int Id;
//this is the FK
public int Item1Id {get;set;}
public Item1 Item1 {get;set;}
//Not in db. Ignored field in EntityTypeConfiguration
public int Item3Count;
public List<Item3> Item3List { get; set; }
}
public class Item3{
public int Id;
//this is the FK
public int Item2Id {get;set;}
public Item2 Item2 {get;set;}
}
我希望能够返回Item1列表和相关的Item2列表,并加载与Item2关联的Item3List计数,而不需要加载Item3List。
这是我目前正在进行的操作:
public IEnumerable<Item1> GetItems()
{
return base.Query().Include(item1 => item1.Item2List.Select(item2 => item2.Item3List)).ToList();
}
这段代码返回了三个对象Item1、Item2和Item3的列表。但我只需要Item3Count中Item3List的计数,而不是整个Item3List列表。我该怎么做?我尝试了下面的代码,但它会出现错误。
return base.Query().Include(item1 => item1.Item2List.Select(item2 => new Item2 {
Item3Count = item2.Item3List.Count()
})).ToList();
Include路径表达式必须引用类型上定义的导航属性。对于引用导航属性,请使用点路径表示;对于集合导航属性,请使用Select运算符。参数名称:路径。