0

I have the table structure as below,

enter image description here

I need the result like :

Redbook
  |______Wedding
            |______Christian
            |______Muslim
            |______Hindu

  |______Birthday
  |______Baptism 

Which means, parentID having 0 should be the root and who's id(here 1) as parentID are its children (ie., wedding,birthday,Baptism) and so on... I tried something like:

public List<specdetails> getProductDet(int ID)
         {

             List<specdetails> result;


             using (APM context = new APM())
             {

                 var res = (from s in context.M_CategoryDetails where s.CategoryID == ID  select s).ToList();



                result = res
                    .GroupBy(u=>u.ParentID)
                     .Select(grp => new specdetails
                     {
                         parentID = (int)grp.Key,
                         //ImageList = grp.ToList()

                         description = grp.FirstOrDefault().Description,
                         ID = grp.FirstOrDefault().ID,
                         catID = (int)grp.FirstOrDefault().CategoryID,
                         vPath = grp.FirstOrDefault().VirtualPath

                     }
                     ).ToList();
             }

             return null; //result;
         }

But it does not accomplish my needs.Please help.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
neethu
  • 193
  • 1
  • 3
  • 14
  • Possible duplicate of [LINQ recursive query to return hierarchical set of groups](http://stackoverflow.com/questions/20004587/linq-recursive-query-to-return-hierarchical-set-of-groups) – Zein Makki Jul 27 '16 at 05:46
  • I've had something like that recently here http://stackoverflow.com/questions/38394626/display-comment-in-nested-way-like-gmail-comment/38397405 and here http://stackoverflow.com/questions/38429609/sort-items-by-parent-relationship – fubo Jul 27 '16 at 05:49
  • I've simply done that in two steps: first, I get a flat representation from the database, then I build up the object tree. This is very fast, imo. – Rob Jul 27 '16 at 05:51
  • @Robert How you accomplish the need,can you share the code – neethu Jul 27 '16 at 09:41

0 Answers0