2014-11-25 4 views
2

여기에 json을 변환 할 데이터 테이블에 문제가 있습니다. 이것은, SearchCollection데이터 테이블의 Json 데이터

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

내가 저장 프로 시저에서 데이터를 수집하고, 데이터 테이블에 밀려있다라는 내 수업 이유 ConvertToDatatable()을 사용하고 이잖아, 그 시간은 내가 3 개 테이블 데이터

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; 
} 
를 포함하는 데이터 테이블 얻었다

여기에는 3 개의 테이블 데이터가 들어 있습니다.

그래서 .. 이것은 데이터를 그룹화하려고 시도했지만 여기 카테고리 상단에있는 카테고리와 같은 대답을 얻는 중 booktype과 booktype이 클래스 이름 목록을 보여 주지만 데이터 셋이 필요합니다 category {}, booktype {}, classnames {}

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"] 
     }), 
    }), 
}); 



    Rusult 
This is my result 
{ CategoryId:1 CategoryName:CD  ClassGroupId:15  ClassName:I  ClassNumber:1 BookTypeId:1 BookType:General CD} 
{ CategoryId:2 CategoryName:DVD  ClassGroupId:16 ClassName:II ClassNumber:2 BookTypeId:2 BookType:General DVD} 
{ CategoryId:3 CategoryName:Book ClassGroupId:17 ClassName:III ClassNumber:3 BookTypeId:3 BookType:General Books} 

But i want the result like this 

+ Category={ CategoryId:1 CategoryName:CD  
    CategoryId:2 CategoryName:DVD  
    CategoryId:3 CategoryName:Book } 

    ClassGroup={ClassGroupId:15 ClassName:I  ClassNumber:1 
    ClassGroupId:16  ClassName:II ClassNumber:2 
    ClassGroupId:17  ClassName:III ClassNumber:3} 

BookType{ BookTypeId:1 BookType:General CD 
    BookTypeId:2 BookType:General DVD 
    BookTypeId:3 BookType:General Books 
} 

여기 내 booktype은 booktype 아래 카테고리 및 classname입니다. 하지만 그 결과를 단일 json의 레코드 세 그룹과 같이 하나의 범주 그룹화 컬렉션, 클래스 그룹화 된 컬렉션 및 단일 json 데이터의 책 유형 컬렉션과 같은 도움이 필요합니다.

+0

당신이 DataTable의에서이 있고 JSON 당신이 데이터로부터 생성 할 결과 데이터의 예를 제공하기 위해 질문을 편집 할 수 있습니까? 당신이 무엇을 요구하고 있는지 분명하지 않습니다. –

+0

나는 내 질문을 업데이트 해 주셨습니다. .. –

답변

0

이게 당신이 찾고 있는게 있나요?

var result = new 
    { 
     Category = rows 
      .GroupBy(r => new 
      { 
       x = r["CategoryId"], 
       y = r["CategoryName"] 
      }) 
      .Select(g => new 
      { 
       CategoryId = g.Key.x, 
       CategoryName = g.Key.y 
      }), 
     ClassGroup = rows 
      .GroupBy(r => new 
      { 
       x = r["ClassGroupId"], 
       y = r["ClassName"], 
       z = r["ClassNumber"] 
      }) 
      .Select(g => new 
      { 
       ClassGroupId = g.Key.x, 
       ClassName = g.Key.y, 
       ClassNumber = g.Key.z 
      }), 
     BookType = rows 
      .GroupBy(r => new 
      { 
       x = r["BookTypeId"], 
       y = r["BookType"] 
      }) 
      .Select(g => new 
      { 
       BookTypeId = g.Key.x, 
       BookType = g.Key.y 
      }) 
    }; 

바이올린 : https://dotnetfiddle.net/h9qXqc

+0

네, 감사합니다. Brian Rogers, –