【发布时间】:2010-03-26 10:47:13
【问题描述】:
我正在构建一个时间报告软件
我有一个Dictionary<string, Dictionary<string, double>>。主字典中的键是用户名,它们的值是 .
我有一个函数 GetDepartment(string UserName),它返回用户部门的字符串。
我想要的是创建一个相同类型的新字典,其中部门作为主键,在子字典中,小时是该部门的总数。
我一直在尝试使用 linq 执行此操作,但没有成功。很高兴在这里得到一些帮助!
编辑:这段代码完全符合我的要求。但我想在 LINQ 中使用它
Dictionary<string, Dictionary<string, double>> temphours = new Dictionary<string, Dictionary<string, double>>(); ;
foreach (var user in hours)
{
string department = GetDepartment(user.Key);
if (!temphours.ContainsKey(department))
{
temphours.Add(department, new Dictionary<string, double>());
}
foreach (var customerReport in user.Value)
{
if (!temphours[department].ContainsKey(customerReport.Key))
{
temphours[department].Add(customerReport.Key, 0);
}
temphours[department][customerReport.Key] += customerReport.Value;
}
}
【问题讨论】:
-
"它们的值是...的字典"什么?
-
您确定您使用的是 LINQ to Entities(如您的标题所示)而不是 LINQ to Objects?
-
@ jens 修正了标题(我错了)。 @Marcelo:这是一个键值对。键值对有一个键和一个值