2014-10-02 3 views
0

내가하고 싶은 그룹화에 대해 LINQ 표현식을 사용하는 데 약간의 문제가 있습니다. 나는 아마도 보내야 할 수천 개의 메일 목록을 가지고있을 것이고 나는 그들을 2000 개의 메일 목록으로 그룹화하여 이것을 부모 목록에 넣을 것입니다. 원래의 목록은 다음과 같습니다 List<Recipient>()GroupBy로 목록 요소 그룹화

받는 사람, 주소 이름이 등

내가 foreach는 함께하고 싶은,하지만 쓴

수 낫다고 생각은 LINQ로 갖고 싶은 경우 표현, 나는 그것을 알아낼 수 없다.

private List<List<string>> PageList(List<string> recipients) 
    { 
     return recipients.Select((x, i) => new { Index = i, Value = x }) 
             .GroupBy(x => x.Index/2000) 
             .Select(x => x.Select(v => v.Value).ToList()) 
             .ToList(); 
    } 

내가 같은 시도했지만 LINQ가 난 인터뷰 나받는 사람의 목록에서 선택할 수 있습니다 :

private List<List<Recipient>> PhysicalPageList(List<Recipient> recipients) 
    { 
     var pageList = new List<List<Recipient>>(); 
     var smallList = new List<Recipient>(); 
     foreach (var rec in recipients) 
     { 
      smallList.Add(rec); 
      if (smallList.Count % 2000 == 0) { continue; } 
      var tmpList = new List<Recipient>(); 
      tmpList.AddRange(smallList); 
      pageList.Add(smallList); 
      smallList.Clear(); 
     } 
     if (smallList.Count() != 0) 
     { 
      var tmpList = new List<Recipient>(); 
      tmpList.AddRange(smallList); 
      pageList.Add(tmpList); 
      smallList.Clear(); 
     } 

     return pageList; 
    } 

나는 (이 서면 havent 한 자신) 문자열이 아닌받는 사람이의 예를 가지고있다.

답변

1

왜이 제품을 사용하지 않으시겠습니까?

private List<List<Recipient>> PageList(List<Recipient> recipients) 
{ 
    return recipients.Select((x, i) => new { Index = i, Value = x }) 
        .GroupBy(x => x.Index/2000) 
        .Select(x => x.Select(v => v.Value).ToList()) 
        .ToList(); 
} 
+0

그래, 나는 그걸로도 깨달았습니다 ... 처음에는 어디가 잘못되었는지 모르지만 지금은 작동합니다 ... – Todtaure

+0

@Todtaure 고마워! 때때로 일어난다. 내가 도왔다 니 기쁘다. – Christos