C# : Merging the Two Dictionaries with respect to Values using LINQ
<p>I have two dictionaries in c#.</p>

<p>The Two Dictionaries and their calues are</p>

<pre><code>Dictionary&lt;int,List&lt;string&gt;&gt; D1 = new Dictionary&lt;int,List&lt;string&gt;&gt;();
Dictionary&lt;int,List&lt;string&gt;&gt; D2= new Dictionary&lt;int,List&lt;string&gt;&gt;();
Dictionary&lt;int,List&lt;string&gt;&gt; D3 new Dictionary&lt;int,List&lt;string&gt;&gt;();


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

<p>Where 1,2,3 and 4 are keys of Dictionary D1</p>

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

<p>Where 1,2,3,4,5,6 and 7 are keys of Dictionary D2</p>

<p>Then the output Dictionary Contains this values,</p>

<pre><code>  D3[1] = {"a","b","h"}
  D3[2] = {"c","d"}
  D3[3] = {"e","f","l"}
</code></pre>

<p><strong>Note: 
    Please take the Input Dictionary with values greater than 1.Thats why  i am eliminating the D1[4] , D2[4] and D2[7]</strong></p>

<p>Here is my code : </p>

<pre><code>         List&lt;int&gt; l_lstTempNets = new List&lt;int&gt;(D1.Keys);
        int l_nCount = 0;
        for (int l_nData = 0; l_nData &lt; l_lstTempNets.Count; l_nData++)
        {
            D3.Add(l_nCount, D1[l_lstTempNets[l_nData]]);
            l_nCount++;
        }
        l_lstTempNets = new List&lt;int&gt;(D2.Keys);
        for (int l_nData = 0; l_nData &lt; l_lstTempNets.Count; l_nData++)
        {
            D3.Add(l_nCount, D2[l_lstTempNets[l_nData]]);
            l_nCount++;
        }





        List&lt;int&gt; l_lstOuter = new List&lt;int&gt;(D3.Keys);
        List&lt;int&gt; l_lstInner = new List&lt;int&gt;(D3.Keys);
        for (int l_nOuter = 0; l_nOuter &lt; l_lstOuter.Count; l_nOuter++)
        {
            if (D3.ContainsKey(l_lstOuter[l_nOuter]) == false)
                continue;
            List&lt;string&gt; 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 &lt; l_lstInner.Count; l_nInner++)
            {
                if (l_lstOuter[l_nOuter] != l_lstInner[l_nInner])
                {
                  if (D3.ContainsKey(l_lstInner[l_nInner]) == false)
                        continue;
                 List&lt;string&gt; l_lstInnerValue = new List&lt;string&gt;(D3[l_lstInner[l_nInner]]);
                    l_lstInnerValue.Sort();
                    for (int l_nOuterData = 0; l_nOuterData &lt; l_lstOuterValue.Count; l_nOuterData++)
                    {
                        if (l_lstInnerValue.Contains(l_lstOuterValue[l_nOuterData]))
                        {
                            for (int l_nInnerData = 0; l_nInnerData &lt; 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]);
                }
            }

        }
</code></pre>

<p>Is it possible using LINQ
If you have any Queries ,Plz Let me know</p>
