我没有找到一种优雅的Linq方式来检索分配给人员列表中的人员的最大日期,这些人员被分配到任务中。
我的代码是
public class Mission
{
public string MissonAssignedTo { get; set; }
public DateTime Date { get; set; }
}
public Dictionary<string, Dictionary<string, List<Mission>>> MissionList =
new Dictionary<string, Dictionary<string, List<Mission>>>();
我有这种价值的字典
MissionList[0] Key = MissionImpossible
Value = count 1
Key=Sean
Value=count 7
[0] = date = 10/01/2017
[1] = date = 10/01/2016
[2] = date = 10/01/2017
[3] = date = 10/01/2018
[4] = date = 10/01/2015
[5] = date = 10/01/2014
[6] = date = 10/01/2013
MissionList[1] Key = MissionImpossibleThePayPack
Value = count 1
Key=Connery
Value=count 4
[0] = date = 10/01/2017
[1] = date = 10/01/2019
[2] = date = 10/01/2017
[3] = date = 10/01/2018
我需要为每个人和他的最大日期检索字典,就像这样。
dicoResult= {
{Sean,10/01/2018} ,
{Connery,10/01/2019}
}
祝你好运。 Jolynice
答案 0 :(得分:1)
虽然您的自定义类型对我来说很奇怪,但如果我理解正确,那么这应该会给您预期的结果: -
var result = MissionList.SelectMany(x => x.Value)
.ToDictionary(x => x.Key, y => y.Value.Max(z => z.Date));
首先展平内部字典,然后使用ToDictionary
将结果投影到字典中。