Reputation: 1804
Say I have two List<KeyValuePair<string, string>>
a and b where a =
"one", "N",
"two", "N",
"three", "N"
and b =
"one", "Y"
"two", "N"
I want to do a left outer join, but take the value from b for the Value
if there is an entry in b, so the result should be =
"one", "Y" <- this value is taken from b
"two", "N",
"three", "N"
I've tried doing a normal left outer join, but the result for the Value
of "three" is always a blank string
var res = (from l in a
join r in b on l.Key equals r.Key into lrs
from lr in lrs.DefaultIfEmpty()
select new KeyValuePair<string, string>(l.Key, lr.Value)).ToArray();
Upvotes: 2
Views: 121
Reputation: 149010
This should work too:
var res = (from p in b.Concat(a)
group p by p.Key into g
select g.First()).ToArray();
Or in fluent syntax:
var res = b.Concat(a).GroupBy(p => p.Key, (k, g) => g.First()).ToArray();
Upvotes: 5