I am trying to produce a custom entity using a select statement that involves tables a parent/child relation (1:n). I made an attempt to group the objects selected, but I am having trouble coming up with a solution.
My data would be this format:
ParentTable
Userid | Col1 | Col2 | ... | Coln
ChildTable:
Id | Userid | Col1 | Col2 | ... | Coln
The result object I am trying to produce would be this:
Custom { UserID, IEnumerable<ChildTable> }
I have written a query:
from parent in db.ParentTable
join child in db.ChildTable on new { Userid = parent.Id } equals new { Userid = child.Userid } into child_join
from child in child_join.DefaultIfEmpty()
where
child.Col1 == Argument1 &&
child.Col2 == Argument2
select new {
Username = parent.Username,
child.Col1,
child.Col2,
child.Col3,
child.Coln
}
That will return this result:
Username | Child Col1 | Child Col2 | Child Col3 | .. |Child Coln
asdf 1 11 22 .. 33
asdf 2 22 33 .. 44
asdf 3 33 44 .. 55
qwer 4 44 55 .. 66
qwer 5 55 66 .. 77
qwer 6 66 77 .. 88
zxcv 7 77 88 .. 99
zxcv 8 88 99 .. 00
I would like to achieve this:
Username | IEnumerable<Child>
asdf | {{1 11 22 .. 33}
| {2 22 33 .. 44}
| {3 33 44 .. 55}}
qwer | {{4 44 55 .. 66}
| {5 55 66 .. 77}
| {6 66 77 .. 88}}
zxcv | {{7 77 88 .. 99}
{8 88 99 .. 00}}
I am trying to group the items by the username and produce a custom object in the form of Custom { UserID, IEnumerable } where I can serialize the objects once completed. Any help is appreciated. Edit: Regarding the data structure, it can not be changed as I am connecting to a 3rd party system so I am working with what is given.