2016-07-27 4 views
1

모든 시작 나열 결과 (일반 구조) :일반 데이터 소스는 C 번호를 저장 프로 시저 데이터

IdGrandad, GrandadName, IdDad, DadName, IdChild, ChildName

응답은 중첩 된 엔터티 목록이어야합니다.


가 나는 SqlDataReader 개체에 데이터를 얻을 ... 다음, 여기에 루프가 온다 :

var grandadList = new List<Grandad>(); 

while (reader.Read()) 
{ 
    // Properties setters. 
    Grandad grandadItem = BindGrandad(reader); 
    Dad dadItem = BindDad(reader); 
    Child childItem = BindChild(reader); 

    // Entities finders. 
    Func<Grandad, bool> foundGrandad = (item => item.Identity.Equals(grandadItem.Identity)); 
    Func<Dad, bool> foundDad = (item => item.Identity.Equals(dadItem.Identity)); 

    if (!grandadList.Any(foundGrandad)) 
     grandadList.Add(grandadItem); 

    // Here comes the horror-code... 
    if (!grandadList.Where(foundGrandad).SelectMany(item => item.DadList).ToList().Any(foundDad)) 
     grandadList.SingleOrDefault(foundGrandad)? 
      .DadList.Add(dadItem); 

    grandadList.SingleOrDefault(foundGrandad)? 
     .DadList.SingleOrDefault(foundDad)? 
     .ChildList.Add(childItem); 
} 



내가 루프 코드를 단순화 할 수있는 방법의 어떤 생각?

미리 감사드립니다.

답변

0

아, 전혀 대답이 없습니다.

글쎄, 난 그냥 먼저 일반 객체를 생성한다, 다음 .GroupBy() 트릭을 할 보자 ...

을에 "더 나은"해결책을 발견했습니다!

나중에 읽으십시오.