2013-06-19 2 views
2

데이터를 속성 값으로 목록으로 분할하고 목록 항목 간의 모든 조합 옵션을 확인하고 싶습니다. 내 문제는 내가 얼마나 많은 목록을 얻을지 모르며 이런 식으로 옆에 할 수있는 더 좋은 방법이 있는지 알 수 없다 :C의 n 목록에 대한 모든 목록 항목 조작 옵션 #

var a = Data.Descendants ("value"). 여기서 x => x.Attribute ("v"). 값 == "1"). ToList(); var b = Data.Descendants ("value"). 여기서 (x => x.Attribute ("v"), 값 == "2") ToList(); var c = Data.Descendants ("value"). 여기서 (x => x.Attribute ("v"), 값 == "3"). ToList();

의 foreach (a에서 VAR의 tempA) { 의 foreach (b에서 VAR의 tempB) { 의 foreach (c에서 VAR의 tempC) { 것을 않는다 } } }

편집 : 나는 하나의 데이터 소스에서 내 항목을 확인하고 싶습니다 (var items = new List<string>{"1","1","2","3","2","1","3","3","2"})

이제

나는 3 개 목록 (list a = "1","1","1" - list b = "2","2","2" - list c = "3","3","3")

이 목록을 분할하고 싶습니다

이 단계에서 내가하려는 것은 한 목록의 한 항목에서 다른 목록의 다른 항목까지 모든 조합을 확인하는 것입니다.

a[0] with b[0] c[0] 
a[0] with b[0] c[1] 
a[0] with b[0] c[2] 
a[0] with b[1] c[0] 
. 
. 
b[1] with a[2] c[2] 
. 
. 

고마워요!

답변

0

LINQ GroupBy 메서드를 사용해 볼 수 있습니까? 몇 가지 예는 위치 : 그룹에 요소를 GroupBy을 사용할 수 있습니다

LINQ GroupBy examples

+0

을 할 것입니다 문자열의 새 편집 목록 romoku의 생각과 chrisC

//new list of lists to hold new information. List<List<Descendants>> NewList = new List<List<Descendants>>(); foreach (var item in Data.Descendants.GroupBy(x => x.Attribute("v").Value)) { NewList.Add(item.ToList()); } 

의 선 다음,이 작업을 수행 할 수 있습니다 고마워, 그게 첫 번째 문제를 해결합니다. 두 번째 아이디어가 있습니까? 항목들 사이의 모든 조합을 실행하는 방법 – Asaf

+0

@Romoku의 SelectMany/ForEach 제안이이를 해결합니까? – ChrisC

0

. 그런 다음 Linq를 사용하여 조합을 만들 수 있습니다.

var grouping = Data.Descendants("value") 
        .GroupBy(x => x.Attribute("v").Value); 

var combinations grouping.SelectMany(x => 
           grouping.Select(y => 
            new { Group = x, Combination = y })); 

foreach(var c in combinations) 
{ 
    //Do Something 
} 

public class Pair 
{ 
    public string A { get; set; } 
    public string B { get; set; } 
} 

var pairs = new List<Pair>(); 
pairs.Add(new Pair { A = "1", B = "2" }); 
pairs.Add(new Pair { A = "1", B = "3" }); 
pairs.Add(new Pair { A = "1", B = "4" }); 
pairs.Add(new Pair { A = "2", B = "1" }); 
pairs.Add(new Pair { A = "2", B = "2" }); 
pairs.Add(new Pair { A = "2", B = "3" }); 

var grouping = pairs.GroupBy(x => x.A); 

var combinations = grouping.SelectMany(x => 
           grouping.Select(y => 
            new { Group = x, Combination = y })); 

Groupings result

0

당신이 그것에게

List<List<string>> NewList = new List<List<string>>(); 

foreach (var item in OriginalList.GroupBy(x => x)) 
{ 
     NewList.Add(item.ToList()); 
}