Reputation: 2616
My stored procedure returns data in following format
Hierarchy
table:
ParentId ChildId ParentName ChildName
1 9 AAA BBB
1 10 AAA CCC
1 11 AAA DDD
This data can be linked to a main table which holds other properties of parent and child entities such as Name
etc.
Person
table:
Id Name Age
1 AAA 40
Id
in Person
table is linked to Hierarchy
table.
I need to convert this to following
Public Class HierarchyData
{
public Person Parent {get;set;}
List<Person> Children {get;set}
}
I am calling a stored procedure and storing output in var.
this.context.ExecuteQuery<Hierarchy>(MyStoreProc, Paramerters);
Public Class Hierarchy
{
public int ParentId {get;set;}
public int ChildId {get;set;}
}
Can you please suggest how it can be converted to List<HierarchyData>
?
Upvotes: 0
Views: 1289
Reputation: 3950
Say, the list is of type List<Hierarchy>
. Then you may run the following code:
var hierarchyDataList =
list
.GroupBy(x => x.ParentId)
.Select(g => new HierarchyData
{
Parent = new Person
{
Id = g.Key
Name = g.First().ParentName // NAME FIELD ADDED
},
Children = g.Select(x => new Person
{
Id = x.ChildId,
Name = x.ChildName
}
)
}
);
Name
field has been added in each Parent
property of HierarchyData
.
Upvotes: 1