C# : Merging the Two Dictionaries with respect to Values using LINQ
I have two dictionaries in c#.
The Two Dictionaries and their calues are
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"};
Where 1,2,3 and 4 are keys of Dictionary D1
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"};
Where 1,2,3,4,5,6 and 7 are keys of Dictionary D2
Then the output Dictionary Contains this values,
D3[1] = {"a","b","h"}
D3[2] = {"c","d"}
D3[3] = {"e","f","l"}
Note: Please take the Input Dictionary with values greater than 1.Thats why i am eliminating the D1[4] , D2[4] and D2[7]
Here is my code :
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
{
开发者_如何学Python IsExists = false;
}
}
}
else
IsExists = false;
if (IsExists)
{
if (D3.ContainsKey(l_lstInner[l_nInner]))
D3.Remove(l_lstInner[l_nInner]);
}
}
}
Is it possible using LINQ If you have any Queries ,Plz Let me know
From the description of your request, I understood that you need: 1) Filter out any dictionary entry with less than 2 values within the Value list. 2) Match the entries in D2 and D1 so that the first item in D2.Value equals the last item in D1.Value. 3) Store into D3 any D1 entry regardless of the match with D2 entries. 4) Merge the matching entries so that each resulting Value contains all the items from D1.Value and D2.Value.
It seems that the items inside each D1.Value and D2.Value represents some kind of hierarchical system, where the last item in D1.Value links to the first item in D2.Value.
Here follows my code:
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 and f2 are counterparts of D1 and D2 filtered to contain only entries with enough (2+) items. joined will match all f1 and f2 based on the explained rule and will produce anonymous items containing f1.Key, f1.Value, f2.Value. The subsequent loop will add to D3 the required result, checking for f2.Value being null.
Regards, Daniele.
精彩评论