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,516D3[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은 나를 요청에 대한 설명에서
병합 규칙을 이해할 수 없었습니다 – Andrey
왜 사전이 여기에 사용되는지 알 수 없습니다 –
여기에서 무엇을하려고하는지 명확하게 알 수는 없습니다. 처음에는 내가 병합 된 사전을 원했을 것이라고 생각했습니다. 값은 원본 값의 합집합이지만 이제는 연결된 목록을 나타내는 것으로 생각하십니까? 또는 뭔가? – AakashM