0

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>?

  • Can you show some more code, especially the one that reads the data from the stored procedure? What have you tried so far? – Markus Mar 14 '14 at 11:43
  • So your getting two ids from your database and you want to store them as these Person objects? where's the rest of the data coming from (name and age) or is that still in the database? – GrahamHull Mar 14 '14 at 11:59
  • If you are going to read a stored procedure to convert the result sets into a list of IEnumerable objects, why not map this in EF directly? You would have the entities already, and instead of using stored procedures, you would use Linq to SQL directly? You are tyring to achieve the same thing, yet the other way around. – Mez Mar 14 '14 at 12:01
  • Check here : http://stackoverflow.com/questions/20916873/entity-framework-code-first-self-referencing-parent-child-with-payload. Doing the same thing, however from mapped entities directly. – Mez Mar 14 '14 at 12:04
  • My Store Proc does Recursive CTEs (Queries) to do some business logic. I have changed my store proc to return Parent Name and Child Name as Well. All i want now is to somehow convert that to List of HierarchyData class. Some Sort of GroupBy ParentId thing is Required here Perhaps. – InTheWorldOfCodingApplications Mar 14 '14 at 12:09

1 Answers1

1

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
                                                   }
                                         )
                  }
        );

EDIT

Name field has been added in each Parent property of HierarchyData.

Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20