代码之家  ›  专栏  ›  技术社区  ›  Oskar Kjellin

Linq到对象分组依据

  •  1
  • Oskar Kjellin  · 技术社区  · 14 年前

    我在建一个报时软件

    我有一个 Dictionary<string, Dictionary<string, double>> . 主字典中的键是用户名,其值是的字典。

    我有一个函数GetDepartment(stringusername),它返回一个带有用户部门的字符串。

    我想要的是装箱一本新字典,类型相同,以部门为主键,在子字典a中,小时数是该部门的总小时数。

    我一直在尝试用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;
                }
            }
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Jens    14 年前

    下面的表达式在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
  •  1
  •   Amy B    14 年前

    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
    );