I've got the following class
public class Application
{
public int Id { get; set; }
public int Version { get; set; }
(...)
}
And I have the following IEnumerable<Application>
:
IEnumerable<Application> applications1 = new List<Application>
{
new Application {Id = 1, Version = 1},
new Application {Id = 2, Version = 1},
new Application {Id = 3, Version = 3}
};
IEnumerable<Application> applications2 = new List<Application>
{
new Application {Id = 1, Version = 2},
new Application {Id = 3, Version = 2}
new Application {Id = 4, Version = 1}
};
How can I merge them into a single IEnumerable<Application>
using LINQ while ensuring that if two Applications
have the same Id
only the one with the highest Version
is added to the new IEnumerable<Application>
, effectively my final `IEnumerable should be equivalent to:
IEnumerable<Application> final = new List<Application>
{
new Application {Id = 1, Version = 2},
new Application {Id = 2, Version = 1},
new Application {Id = 3, Version = 3},
new Application {Id = 4, Version = 1}
}