2017-02-14 99 views
0

我找不到優雅的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

+1

你可以提供有效的C#代碼字典樣本嗎?並顯示你到目前爲止已經嘗試過 –

回答

1

雖然你的自定義類型看起來怪我,如果我明白你正確,則這應該給您預期的結果: -

var result = MissionList.SelectMany(x => x.Value) 
         .ToDictionary(x => x.Key, y => y.Value.Max(z => z.Date)); 

首先拼合你內心的字典,最後項目的結果到字典使用ToDictionary

Sample Fiddle.

+1

謝謝Rahul,你是我的自定義類型很奇怪,我想我會改變它更精確。 – jolynice