【发布时间】:2021-11-17 14:41:29
【问题描述】:
我目前正在寻找一种可以转换
IEnumerable<DateTimeInterval> 到 Dictionary<Guid, IEnumerable<DateTimeInterval>>
我尝试使用IEnumerable<DateTimeInterval>.ToDictionary(x => x.id)
但这只是返回一个Dictionary<Guid, DateTimeInterval> 而不是想要的Dictionary<Guid, IEnumerable<DateTimeInterval>>
我做错了什么?
dateTimeInterval 是这样定义的:
public class DatetimeInterval
{
public Guid key {get; set;}
public DateTime From { get; set; }
public DateTime To { get; set; }
public DatetimeInterval(DateTime from, DateTime to, Guid key)
{
Key = key;
From = from;
To = to;
}
}
在IEnumerable<DateTimeInterval> 中可能存在具有相同键的 DateTimeIntervals。
因此我非常希望拥有
IEnumerable.ToDictionary(x => x.key, v => v)
返回
但这只是返回一个Dictionary<Guid, DateTimeInterval> 而不是想要的Dictionary<Guid, IEnumerable<DateTimeInterval>>
【问题讨论】:
-
stuff.ToDictionary(k => new Guid(...), v => v)? -
@aybe DatetimeInterval 本身有一个id,其中一些是相同的,因此应该存储在列表中
-
向您的问题添加更多代码,因为不清楚。
-
@aybe 添加了更多代码.. 不确定这里有什么不确定..
-
您是否在寻找
ToLookup而不是ToDictionary?字典是 1:1 键:值,而听起来您正在寻找 1:许多键:值,因为DateTimeInterval可以有重复的 ID
标签: c# linq dictionary