Merging two classes into a Dictionary using LINQ
c#, dictionary, linq, list
Solution
Sounds like you could do:
// Project both lists (lazily) to a common anonymous type
var anon1 = list1.Select(foo => new { foo.Id, foo.Information });
var anon2 = list2.Select(bar => new { bar.Id, bar.Information });
var map = anon1.Concat(anon2).ToDictionary(x => x.Id, x => x.Information);
(You could do all of this in one statement, but I think it's clearer this way.)
Problem
I have two classes which share two common attributes, Id and Information. ``` public class Foo { public Guid Id { get; set; } public string Information { get; set; } ... } public class Bar { public Guid Id { get; set; } public string Information { get; set; } ... } ``` Using LINQ, how can I take a populated list of Foo objects and a populated list of Bar objects: ``` var list1 = new List<Foo>(); var list2 = new List<Bar>(); ``` and merge the Id and Information of each into a single dictionary: ``` var finalList = new Dictionary<Guid, string>(); ``` Thank you in advance.