我有以下代码:
private IDictionary<Guid, JobStatistic> GetAgentsStatistics(IList<Guid> agentIds, DateTime startTime, DateTime endTime)
{
...
IDictionary<Guid, JobStatistic> jobStatistics = reportData.GroupBy(item => item.AgentId)
.ToDictionary(items => items.Key, items => items.ToJobsStatistic());
// Some agents might not have any records in specified period of time
var missingAgents = agentIds.Except(jobStatistics.Keys);
missingAgents.ForEach(agentId => jobStatistics.Add(agentId, new JobStatistic()));
return jobStatistics;
}
但现在我更改了我的DataContract(报告数据),我需要使用item.AgentIds
而不是item.AgentId
。如何更改代码以使用一组元素执行相同的操作?
答案 0 :(得分:1)
我可能会使用不同形式的LINQ。我觉得这样的事情可行:
var jobStatisticsResult = from data in reportData
from agentId in data.AgentIds
group data by agentId
into grp
select new { AgentId = grp.Key, Items = grp.ToList() };
var jobStatistics = jobStatisticsResult.ToDictionary(x => x.AgentId, x => x.Items.ToJobsStatistic());