Reputation: 5469
Hi I have two Dictionaries and I need to find a way to join them together.Here are the two dictionary types.
IDictionary<string, byte[]> dictionary1
IDictionary<IFileShareDocument, string> dictionary2
Out of this two dictionaries I have to create a third dictionary that will look like this
IDictionary<IFileShareDocument, byte[]> dictionary3
Both dictionaries have the exact same number of items and the string property of both them is the linking point.
What I would like is to be able to write something that would do somethign like this:
dictionary1.value join with dictionary2.key
where dictionary1.key == dictionary2.value
This statement should result in dictionary3.
Is there any way I can achieve this I can not seem to find a way to do this?
Upvotes: 1
Views: 1067
Reputation: 56586
Here's a way to do it using the LINQ query syntax, with join
(this compiles to roughly the same thing as @KingKing's solution):
IDictionary<IFileShareDocument, byte[]> dictionary3 =
(from item1 in dictionary1
join item2 in dictionary2 on item1.Key equals item2.Value
select new { item2.Key, item1.Value })
.ToDictionary(x => x.Key, x => x.Value);
Note that the above is greatly preferred to this example using from
and where
, because it is more efficient. I'm including this here because if you're like me (more familiar with SQL, which would convert something like this to a join automatically), this poor way might be the first one that comes to mind:
IDictionary<IFileShareDocument, byte[]> dictionary3 =
(from item1 in dictionary1
from item2 in dictionary2
where item1.Key == item2.Value
select new { item2.Key, item1.Value })
.ToDictionary(x => x.Key, x => x.Value);
Upvotes: 2
Reputation: 117175
Would this work for you?
var result =
dictionary2
.ToDictionary(x => x.Key, x => dictionary1[x.Value]);
Upvotes: 2
Reputation: 63387
var dictionary3 =
dictionary1
.Join(dictionary2, x => x.Key, x => x.Value, (x, y) => new { x, y })
.ToDictionary(a => a.y.Key, a => a.x.Value);
Upvotes: 5