6

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
rboarman
  • 8,248
  • 8
  • 57
  • 87

4 Answers4

16
from id in idList.Keys
where lookupList.ContainsKey(id)
let value1 = idList[id]
let value2 = lookupList[id]
select new {id, value1, value2}

Or, more classically

from kvp1 in idList
join kvp2 in lookupList on kvp1.Key equals kvp2.Key
select new {key = kvp1.Key, value1 = kvp1.Value, value2 = kvp2.Value}

The mistake in your query is one of scoping:

from a in theAs
join b in theBs on (leftside) equals (rightside)

a is in scope in the leftside area. b is in scope in the rightside area.

Amy B
  • 108,202
  • 21
  • 135
  • 185
1

I apologize if I misinterpretted your question, but do you just want to retrieve the Values from list B only where list A has a KeyValuePair with the same Key?

from lookup in lookupList
where idList.Keys.Contains(lookup.Key)
select lookup.Value;
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • I'll take a vote up but go ahead and accept David B's answer. He beat me to it fair and square with a lot more detail. :) – Nathan Taylor Jun 16 '10 at 21:18
  • Is it safe to add .AsQueryable() to the end of the statement? Are there performance concerns by doing so? The lookup collection is quite large. – rboarman Jun 16 '10 at 21:24
  • @rboarman: answers are sorted by score, not by time. See "answered ... ago" in bottom right corner to see when the answer was given. – Pavel Minaev Jun 16 '10 at 21:31
  • It is safe to add `.AsQueryable()` in a sense that you will ultimately get the same result if you try to enumerate it. Performance concerns depend on how you're actually planning to use it (there must be a reason why you want to do it, right?). – Pavel Minaev Jun 16 '10 at 21:32
  • For what purpose do you need it AsQueryable()? – Nathan Taylor Jun 16 '10 at 21:32
0
        var q = from id in idList
                join entry in lookupList
                  on id.Key equals entry.Key
                select entry.Value;

Your desired linq statement will look like that, ID and Entry needed to be switched around on the condition.

Meiscooldude
  • 3,671
  • 5
  • 27
  • 30
0

What do you think of this ?

var values = idList.Keys.Select(i => lookupList[i]);
Marcote
  • 2,977
  • 1
  • 25
  • 24
  • Ah, I see. you have non matchings things. I should change it to Keys.Contains. Anyway, the Nathan's is answer is the way to go. – Marcote Jun 16 '10 at 21:21