我有3个收集清单如下.
public static List<Thing> English = new List<Thing>
{
new Thing {ID = 1, Stuff = "one"},
new Thing {ID = 2, Stuff = "two"},
new Thing {ID = 3, Stuff = "three"}
};
public static List<Thing> Spanish = new List<Thing>
{
new Thing {ID = 1, Stuff = "uno"},
new Thing {ID = 2, Stuff = "dos"},
new Thing {ID = 3, Stuff = "tres"},
new Thing {ID = 4, Stuff = "cuatro"}
};
public static List<Thing> German = new List<Thing>
{
new Thing {ID = 1, Stuff = "eins"},
new Thing {ID = 2, Stuff = "zwei"},
new Thing {ID = 3, Stuff = "drei"}
};
Run Code Online (Sandbox Code Playgroud)
在运行时期间,列表的长度可能会有所不同.例如,德语可能有5个值,英语有2个,西班牙语有1个.
我需要找到哪个列表具有最大值,并需要以下面的格式获得输出.
Id English German Spanish
1 one eins uno
2 two zwei dos
3 three drei tres
4 cuatro
Run Code Online (Sandbox Code Playgroud)
你能帮我解决一下吗?
试试这个:
English.Select(t => new Tuple<Thing,int>(t, 1)).Concatenate(
German.Select(t => new Tuple<Thing,int>(t, 2)).Concatenate(
Spanish.Select(t => new Tuple<Thing,int>(t, 3))
)
).GroupBy(p => p.Item1.ID)
.Select(g => new {
Id = g.Key
, English = g.Where(t => t.Item2==1).Select(t => t.Item2.Stuff).SingleOrDefault()
, German = g.Where(t => t.Item2==2).Select(t => t.Item2.Stuff).SingleOrDefault()
, Spanish = g.Where(t => t.Item2==3).Select(t => t.Item2.Stuff).SingleOrDefault()
});
Run Code Online (Sandbox Code Playgroud)
我们的想法是用原始项目(1英语,2德语,3西班牙语)标记原始项目,按ID分组,然后使用我们在第一步中添加的标记来提取各种语言的详细信息.