【发布时间】:2019-12-12 08:21:40
【问题描述】:
我有一个TimeEntry class 和一个Dictionary,其中Id 作为键,其他属性作为值。现在我想按Category 的值对字典进行分组
class TimeEntry
{
public int Id { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Task { get; set; }
public string Category { get; set; }
public override string ToString()
{
return string.Format($"Start Time: {StartTime}, EndTime: {EndTime}, Task: {Task}, Category {Category}");
}
}
private readonly IDictionary<int, TimeEntry> _timeEntries;
我想显示如下内容:
Household
00:30:00 do something
00:30:00 do some stuff
work
01:30:00 go to work
00:30:00 to some work thing
必须按类别分组
【问题讨论】:
-
每个值都有不同的键
-
采用
Values并使用LINQ 的GroupBy可能是一种简单的方法。然后将输出转换为您想要的任何格式,例如列表字典 -
我已经尝试过使用 LINQ,但我不太擅长
标签: c# linq dictionary group-by