[英]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.