我把我的问题简化了
public class Application
{
public int Id { get; set; }
public int Version { get; set; }
public int Status { get; set; }
}
我的3个 list :
IEnumerable<Application> applications1 = new List<Application>
{
new Application {Id = 1, Version = 1, Status = 1},
new Application {Id = 2, Version = 1, Status = 1},
new Application {Id = 3, Version = 3, Status = 1}
};
IEnumerable<Application> applications2 = new List<Application>
{
new Application {Id = 1, Version = 2, Status = 2},
new Application {Id = 2, Version = 2, Status = 2},
new Application {Id = 3, Version = 1, Status = 0}
};
IEnumerable<Application> applications3 = new List<Application>
{
new Application {Id = 1, Version = 5, Status = 1},
new Application {Id = 2, Version = 0, Status = 1},
new Application {Id = 3, Version = 6, Status = 1}
};
我想制作这个:
IEnumerable<Application> applications4 = new List<Application>
{
new Application {Id = 1, Version = 5, Status = 2},
new Application {Id = 2, Version = 2, Status = 2},
new Application {Id = 3, Version = 6, Status = 1}
};
即具有the highest value of Version and Status个的一个列表. 我试过用LINQ,但我不明白.
我最好的就是这个,但它只能得到最高版本.当有两个属性可供 Select 时,我不明白我该怎么办:
IEnumerable<Application> applications4 = applications1
.Concat(applications2)
.Concat(applications3)
.GroupBy(a => a.Id)
.Select(g => g.Aggregate((acc, curr) => acc.Version > curr.Version ? acc: curr ))
.ToList();