我想将两个具有不同属性的列表合并到一个列表中,但是在合并它时,我想检查在这个特定示例中两个列表中是否有确切的日期,如果有,我想从该元素中获取两个属性,并将它们合并到另一个列表中的一个元素中
列表 1:
List<object> r1 = (from x in sp1 select new
{
x.Imported,
x.Period
}).ToList<object>();
L1 结果:
列表 2:
List<object> r2 = (from x in sp2 select new
{
x.Dissolution,
x.Period
}).ToList<object>();
L2 结果:
想要的结果:
现在,这是我合并 r1 和 r2 的方式:
List<object> r3 = new List<object>(r1.Concat(r2));
最佳答案
你可以将它们转换成相同的类型并使用这样的东西
r1
.Select(x => new { Imported = x.Imported, Dissolution = null, Period = x.Period)
.Concat(
r2.Select(x => new { Imported = null, Dissolution = x.Dissolution, Period = x.Period))
.GroupBy(x => x.Period)
.Select(x => new { Imported = x.Max(e => e.Imported),
Dissolution = x.Max(e => e.Dissolution),
Period = x.Key);
关于c# - 在 C# 中合并两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24652516/