Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
this is my list

C#
public class Collection
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public int ClassGroupId { get; set; }
        public string ClassName { get; set; }
        public int ClassNumber { get; set; }
        public int BookTypeId { get; set; }
        public string BookType { get; set; }
    }



and i have data table collection

C#
static DataTable ConvertToDatatable(IEnumerable<SearchCollection> list)
 {
     var dt = new DataTable();

     dt.Columns.Add("CategoryId");
     dt.Columns.Add("CategoryName");
     dt.Columns.Add("ClassGroupId");
     dt.Columns.Add("ClassName");
     dt.Columns.Add("ClassNumber");
     dt.Columns.Add("BookTypeId");
     dt.Columns.Add("BookType");

     foreach (var item in list)
     {
         var row = dt.NewRow();

         row["CategoryId"] = item.CategoryId;
         row["CategoryName"] = item.CategoryName;
         row["ClassGroupId"] = item.ClassGroupId;
         row["ClassName"] = item.ClassName;
         row["ClassNumber"] = item.ClassNumber;
         row["BookTypeId"] = item.BookTypeId;
         row["BookType"] = item.BookType;


         dt.Rows.Add(row);
     }
     return dt;
 }


this contain 3 tables data

So..
C#
var table = ConvertToDatatable(searchCollections);
           var rows = table.Rows.Cast<DataRow>().ToList();
           var result = rows.GroupBy(r => new { x = r["CategoryId"], y = r["CategoryName"] })
               .Select(g => new
               {
                   CategoryId = g.Key.x,
                   CategoryName = g.Key.y,
                   BookTypes = g.GroupBy(r => new { h = r["BookTypeId"], i = r["BookType"] })
                                               .Select(g1 => new
                                               {
                                                   BookTypeId = g1.Key.h,
                                                   BookType = g1.Key.i,

                                                   ClassNames = g1.Select(r => new
                                                   {
                                                       ClassGroupId = r["ClassGroupId"],
                                                       ClassName = r["ClassName"],
                                                       ClassNumber = r["ClassNumber"]

                                                   }),

                                               }),


               });


here my result is booktype is under category and classname under booktype.
but i want the result just like 3 groups of records in single json, any one help
Posted
Updated 24-Nov-14 22:08pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900