2017-01-23 76 views
-5

我不得不執行SQL查詢返回我集團日期>>

List<Dictionary<string, string>> 

的第一個元素是字段,第二個是值的函數。

在特定的情況下,該列表具有以下值:

{ datetime, '01/23/2017 01:10:30' } 
{ datetime, '01/23/2017 10:00:00' } 
{ datetime, '01/23/2017 11:23:15' } 
{ datetime, '01/20/2017 07:13:20' } 
{ datetime, '01/20/2017 08:20:11' } 
{ datetime, '01/21/2017 07:28:29' } 

我需要將其轉換爲:

{ '01/23/2017', { '01/23/2017 01:10:30', ''01/23/2017 10:00:00', '01/23/2017 11:23:15' } } 
{ '01/20/2017', { '01/20/2017 07:13:20', '01/20/2017 08:20:11' } } 
{ '01/21/2017', { '01/21/2017 07:28:29' } } 

我該如何繼續?

+0

了'datetime'是不相關的,對不對? – Tatranskymedved

+0

正確的,它是由查詢返回的字段的只是名字。 – msmosso

+0

從數據庫的響應似乎很奇怪字典...爲什麼名單? – pimbrouwers

回答

3

根據您所提供的數據,下面是做這件事。請注意,這個代碼寫在LinqPad執行,因此.Dump()只能有:

var source = new List<KeyValuePair<string, string>> { 
    new KeyValuePair<string, string>("datetime", "01/23/2017 01:10:30") , 
    new KeyValuePair<string, string>("datetime", "01/23/2017 10:00:00"), 
    new KeyValuePair<string, string>("datetime", "01/23/2017 11:23:15"), 
    new KeyValuePair<string, string>("datetime", "01/20/2017 07:13:20"), 
    new KeyValuePair<string, string>("datetime", "01/20/2017 08:20:11"), 
    new KeyValuePair<string, string>("datetime", "01/21/2017 07:28:29") 
}; 

var list = source.Select (s => s.Value) 
    .GroupBy (s => DateTime.ParseExact(s, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture).Date) 
    .ToList(); 

list.Dump(); 

輸出:

enter image description here