Linq join two dictionaries using a common key

Posted by rboarman on Stack Overflow See other posts from Stack Overflow or by rboarman
Published on 2010-06-16T21:06:18Z Indexed on 2010/06/16 21:12 UTC
Read the original article Hit count: 304

Filed under:
|

Hello,

I am trying to join two Dictionary collections together based on a common lookup value.

        var idList = new Dictionary<int, int>();
        idList.Add(1, 1);
        idList.Add(3, 3);
        idList.Add(5, 5);

        var lookupList = new Dictionary<int, int>();
        lookupList.Add(1, 1000);
        lookupList.Add(2, 1001);
        lookupList.Add(3, 1002);
        lookupList.Add(4, 1003);
        lookupList.Add(5, 1004);
        lookupList.Add(6, 1005);
        lookupList.Add(7, 1006);

// Something like this:
            var q = from id in idList.Keys
                    join entry in lookupList on entry.Key equals id
                    select entry.Value;

The Linq statement above is only an example and does not compile. For each entry in the idList, pull the value from the lookupList based on matching Keys.

The result should be a list of Values from lookupList (1000, 1002, 1004).

What’s the easiest way to do this using Linq?

Thank you,

Rick

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ