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

5
  • Can you show some more code, especially the one that reads the data from the stored procedure? What have you tried so far? Commented Mar 14, 2014 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? Commented Mar 14, 2014 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. Commented Mar 14, 2014 at 12:01
  • Check here : stackoverflow.com/questions/20916873/…. Doing the same thing, however from mapped entities directly. Commented Mar 14, 2014 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. Commented Mar 14, 2014 at 12:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can we replace g.Key?
I have changed this following Id = g.First().ParentId Name = g.First().ParentName
Will this work for All the Parents in the Result Set , confusing as we are using First() ?
yes, it'll work and you can use in that way avoiding g.Key. As we are grouping by the ParentId and moreover, the Hierarchy table contains both ParentId and ParentName for each row, so it is clear that each grouping will contain the list of same ParentId and ParentName. For this reason, First() should work for all the Parents inside each group. Last() will also do the same for the above reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.