I have the following entity:
public class Item
{
public int Id { get; set; }
public int? ParentId { get; set; }
public Item Parent { get; set; }
public List<Item> Children { get; set; }
public double PropertyA { get; set; }
public double PropertyB { get; set; }
...
}
Now I want to query the database and get the data of all nested children. I could achieve this using Eager Loading with Include():
var allItems = dbContext.Items
.Include(x => Children)
.ToList();
But instead of Eager Loading, I want to make the following forecast:
public class Projection
{
public int Id { get; set; }
public List<Projection> Children { get; set; }
public double PropertyA { get; set; }
}
Is it possible to get only the data you need with one choice? We are using Entity Framework 6.1.3.
Edit:
This is what I have tried so far. I really don't know how to say EF to display all the children Projectionjust like their parents.
An unhandled exception of type "System.NotSupportedException" occurred in EntityFramework.SqlServer.dll
: "" LINQ to Entities. , , .
var allItems = dbContext.Items
.Select(x => new Projection
{
Id = x.Id,
PropertyA = x.PropertyA,
Children = x.Children.Select(c => new Projection()
{
Id = c.Id,
PropertyA = c.PropertyA,
Children = ???
})
})
.ToList();