I’m pretty new to LINQ and a lot of modern data-driven application design techniques in general, so this may be a pretty basic question.
I’m trying to create a projection of a couple different Entity Framework entities to a simple presentation model. Let’s say I have the entites Parent (properties are ID, Name, Age) and Child (properties are ID, Name, Age, with a reference to a Parent). I want to project these to PresentationParent and PresentationChild, where all the properties are the same, but PresentationParent has a List. How would I do this in LINQ?
from p in entities.Parent
select new PresentationParent
{
ID = p.ID,
Name = p.Name,
Age = p.Age,
Children = [[?? What goes here ??]]
}
Is this on the right track at all? I can only seem to find examples of simple, flat projections.
Something like this:
However, your entities should come pre-configured with the necessary foreign key relationships already, so you could do something like this:
Of course, that would return all the properties of each child, so you might want to project the children anyway: