【问题标题】:Linq-To-Objects group byLinq-To-Objects 分组方式
【发布时间】: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:这是一个键值对。键值对有一个键和一个值

标签: c# linq group-by


【解决方案1】:

为什么要使用 LINQ 执行此操作?我不认为它会更清晰,而且 LINQ 查询并不那么容易调试。

以下表达式在 LINQ to Entities 中不起作用,因为您不能在那里调用 GetDepartment 等 C# 函数。

Dictionary<string, Dictionary<string, double>> temphours 
    = (from user in hours
       group user by GetDepartment(user.Key) into department
       select new {
          Key = department.Key
          Value = (from userInDepartment in department
                   from report in userInDepartment.Value
                   group report by report.Key into g // To tired to think of a name =)
                   select new {
                       Key = g.Key
                       Value = g.Sum(reportInG => reportInG.Value)
                   }).ToDictonary(ud => ud.Key, ud=> ud.Value);
       }).ToDictonary(u => u.Key, u=> u.Value);

我不确定这是否没有错误。至少它应该让你知道如何做到这一点。

【讨论】:

    【解决方案2】:

    这是我的看法。

    Dictionary<string, Dictionary<string, double>> temphours =
    (
      from user in hours
      let department = GetDepartment(user.Key)
      from customerReport in user.Value
      group customerReport by department
    )
    .ToDictionary(
      g => g.Key,
      g => g.GroupBy(rep => rep.Key).ToDictionary
      (
        g2 => g2.Key,
        g2 => g2.Sum(rep => rep.Value)
      )
    );
    

    这是我能做到的直接。如果您想要更多的描述性,那么这可能会为您做到:

    Dictionary<string, Dictionary<string, double>> temphours =
    (
      from user in hours
      let department = GetDepartment(user.Key)
      from customerReport in user.Value
      group customerReport by department into reportGroup
      select new
      {
        Department = reportGroup.Key,
        Reports =
        (
           from report in reportGroup
           group report.Value by report.Key
        ).ToDictionary(g => g.Key, g => g.Sum())
      }
    )
    .ToDictionary{
      x => x.Department,
      x => x.Reports
    );
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2018-03-03
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多