2010-06-28 8 views
1

C#에는 두 개의 사전이 있습니다.C# : LINQ를 사용하여 값과 관련하여 두 사전 병합

두 개의 사전 및 그들의 calues는 1,2,3 및 4

D2[1] = new List<string>{"a","b"}; 
D2[2] = new List<string>{"c","d"}; 
D2[3] = new List<string>{"e","f"}; 
D2[4] = new List<string>{"g"}; 
D2[5] = new List<string>{"b","h"}; 
D2[6] = new List<string>{"f","l"}; 
D2[7] = new List<string>{"z"}; 

1,2,3,4,5,6 사전 D1의 열쇠

Dictionary<int,List<string>> D1 = new Dictionary<int,List<string>>(); 
Dictionary<int,List<string>> D2= new Dictionary<int,List<string>>(); 
Dictionary<int,List<string>> D3 new Dictionary<int,List<string>>(); 


D1[1] = new List<string>{"a","b"}; 
D1[2] = new List<string>{"c","d"}; 
D1[3] = new List<string>{"e","f"}; 
D1[4] = new List<string>{"h"}; 

입니다 및도 7은 사전 D2의 키,

그렇다면 출력이 사전 값이 포함되어

0,123,516
D3[1] = {"a","b","h"} 
    D3[2] = {"c","d"} 
    D3[3] = {"e","f","l"} 

참고 : 내가 제거하고 왜 1.Thats보다 큰 값으로 입력 사전을주십시오 D1 [4], D2는 [4]와 D2 [7] 여기

내 코드입니다 :

1) 필터링 어떤 사전 전자 :
  List<int> l_lstTempNets = new List<int>(D1.Keys); 
     int l_nCount = 0; 
     for (int l_nData = 0; l_nData < l_lstTempNets.Count; l_nData++) 
     { 
      D3.Add(l_nCount, D1[l_lstTempNets[l_nData]]); 
      l_nCount++; 
     } 
     l_lstTempNets = new List<int>(D2.Keys); 
     for (int l_nData = 0; l_nData < l_lstTempNets.Count; l_nData++) 
     { 
      D3.Add(l_nCount, D2[l_lstTempNets[l_nData]]); 
      l_nCount++; 
     } 





     List<int> l_lstOuter = new List<int>(D3.Keys); 
     List<int> l_lstInner = new List<int>(D3.Keys); 
     for (int l_nOuter = 0; l_nOuter < l_lstOuter.Count; l_nOuter++) 
     { 
      if (D3.ContainsKey(l_lstOuter[l_nOuter]) == false) 
       continue; 
      List<string> l_lstOuterValue = D3[l_lstOuter[l_nOuter]]; 
      l_lstOuterValue.Sort(); 
      if (l_lstOuterValue.Count == 0 || l_lstOuterValue.Count == 1) 
      { 
       D3.Remove(l_lstOuter[l_nOuter]); 
       continue; 
      } 
      for (int l_nInner = 0; l_nInner < l_lstInner.Count; l_nInner++) 
      { 
       if (l_lstOuter[l_nOuter] != l_lstInner[l_nInner]) 
       { 
        if (D3.ContainsKey(l_lstInner[l_nInner]) == false) 
         continue; 
       List<string> l_lstInnerValue = new List<string>(D3[l_lstInner[l_nInner]]); 
        l_lstInnerValue.Sort(); 
        for (int l_nOuterData = 0; l_nOuterData < l_lstOuterValue.Count; l_nOuterData++) 
        { 
         if (l_lstInnerValue.Contains(l_lstOuterValue[l_nOuterData])) 
         { 
          for (int l_nInnerData = 0; l_nInnerData < l_lstInnerValue.Count; l_nInnerData++) 
          { 
           if (l_lstOuterValue.Contains(l_lstInnerValue[l_nInnerData]) == false) 
           { 
            l_lstOuterValue.Add(l_lstInnerValue[l_nInnerData]); 

           } 
          } 
          IsExists = true; 
          break; 
         } 
         else 
         { 
          IsExists = false; 
         } 
        } 

       } 
       else 
        IsExists = false; 
       if (IsExists) 
       { 
        if (D3.ContainsKey(l_lstInner[l_nInner])) 
         D3.Remove(l_lstInner[l_nInner]); 
       } 
      } 

     } 

당신은 어떤 쿼리가있는 경우 LINQ 를 사용 가능, Plz은 나를 요청에 대한 설명에서

+1

병합 규칙을 이해할 수 없었습니다 – Andrey

+1

왜 사전이 여기에 사용되는지 알 수 없습니다 –

+0

여기에서 무엇을하려고하는지 명확하게 알 수는 없습니다. 처음에는 내가 병합 된 사전을 원했을 것이라고 생각했습니다. 값은 원본 값의 합집합이지만 이제는 연결된 목록을 나타내는 것으로 생각하십니까? 또는 뭔가? – AakashM

답변

0

를 알려주십시오, 나는 당신이 필요로하는 것을 이해 값 목록에서 2 개 미만의 값으로 ntry하십시오. 2) D2.Value의 첫 번째 항목이 D1.Value의 마지막 항목과 같도록 D2와 D1의 항목을 일치시킵니다. 3) D2 항목과의 일치 여부와 상관없이 D3에 D1 항목을 저장합니다. 4) 일치하는 항목을 병합하여 각 결과 값에 D1.Value 및 D2.Value의 모든 항목이 포함되도록합니다.

각 D1.Value 및 D2.Value 내부의 항목은 D1.Value의 마지막 항목이 D2.Value의 첫 번째 항목에 연결되는 일종의 계층 적 시스템을 나타내는 것으로 보입니다.

 var f1 = D1.Where(pair => pair.Value.Count >= 2); 
     var f2 = D2.Where(pair => pair.Value.Count >= 2); 

     var joined = from item1 in f1 
        join item2 in f2 on item1.Value.Last() equals item2.Value.First() into DX 
        from itemX in DX.DefaultIfEmpty() 
        select new { Id = item1.Key, Value1 = item1.Value, Value2 = itemX.Value }; 
     foreach (var item3 in joined) 
     { 
      if (item3.Value2 != null) 
       D3.Add(item3.Id, item3.Value1.Concat(item3.Value2.Skip(1)).ToList()); 
      else 
       D3.Add(item3.Id, item3.Value1.ToList()); 
     } 

F1 충분히 (2+) 항목 항목 만 포함 여과 D1와의 대응 D2있다F2 :

여기 내 코드를 따른다. 는이 규칙에 근거하여 설명 모두 F1 및 F2 일치하며 f1.Key,f1.Value, f2.Value 함유 익명 항목을 생성 할 것이다 합류. 후속 루프는 D3에 추가 결과 f2.Valuenull인지 확인합니다.

감사합니다. Daniele.