I have three model classes
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;}
}
I want to return the list of Item1 along with list of associated Item2, and load the COUNT of Item3List associated with Item 2 without loading the Item3List.
Here is what I am doing right now:
public IEnumerable<Item1> GetItems()
{
return base.Query().Include(item1 => item1.Item2List.Select(item2 => item2.Item3List)).ToList();
}
This returns me the list of all 3 objects Item1, Item2 and Item3. But I only need the count of Item3List in Item3Count, and not the entire Item3List list. How can I achieve that? I tried this below, but it throws error.
return base.Query().Include(item1 => item1.Item2List.Select(item2 => new Item2 {
Item3Count = item2.Item3List.Count()
})).ToList();
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path