[英]List of objects to Dictionary with distinct keys and selected values
我有以下類的對象List
:
class Entry
{
public ulong ID {get; set;}
public DateTime Time {get; set;}
}
該列表包含每個ID值的多個對象,每個對象具有不同的DateTime。
我可以使用Linq將此List<Entry>
轉換為Dictionary<ulong, DateTime>
,其中鍵是ID,值是該ID的Min<DateTime>()
嗎?
聽起來你想要按ID分組, 然后轉換為字典,這樣你最終每個ID都有一個字典條目:
var dictionary = entries.GroupBy(x => x.ID)
.ToDictionary(g => g.Key,
// Find the earliest time for each group
g => g.Min(x => x.Time));
要么:
// Group by ID, with each value being the time
var dictionary = entries.GroupBy(x => x.ID, x => x.Time)
// Find the earliest value in each group
.ToDictionary(g => g.Key, g => g.Min())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.