【问题标题】:Pivot data in two nested List<T> with Linq使用 Linq 透视两个嵌套 List<T> 中的数据
【发布时间】:2015-03-30 10:58:15
【问题描述】:

我有以下类结构:

class Employee()
{
  public String Name { get; set; }
  public List<WorkDay> WorkDays { get; set; }
}

class WorkDay()
{
  public DateTime Date { get; set; }
  public Int Hours { get; set; }
}

是否可以使用 Linq 旋转 List&lt;Employee&gt;,所以我的 DataGridView 中有这样的结果:

           | Name    | Name    |...| Name      |
Date       | Hours   | Hours   |   | Hours     |
Date       | Hours   | Hours   |   | Hours     |   
Date       | Hours   | Hours   |   | Hours     |
Date       | Hours   | Hours   |   | Hours     |
...        | Hours   | Hours   |   | Hours     |

这很棘手,因为它是两个嵌套列表,我只找到了非常简单的单个列表的示例。 Is it possible to Pivot data using LINQ?

我已经到了这一点,但还没有完全做到:

var _result = Employees.SelectMany(x => x.WorkDays)
                       .GroupBy(x => x.Date)
                       .Select(y => new
                                    {
                                        DATE = y.Key,
                                        NAME = y.Select(z => z.Employee.Name).ToArray()
                                    })
                       .ToList();

如果有任何建议,我将不胜感激。

【问题讨论】:

    标签: c# linq pivot-table


    【解决方案1】:

    我认为你需要这个:-

    var result = employees.SelectMany(x => x.WorkDays, (employeeObj, workDays) => 
                                                       new { employeeObj, workDays })
                          .GroupBy(x => x.workDays.Date)
                          .Select(x => new
                                 {
                                    Date = x.Key,
                                    NameAndHours = x.Select(z => 
                                        new { 
                                                Name = z.employeeObj.Name, 
                                                Hours = z.workDays.Hours 
                                            })
                                 }).ToList();
    

    这是Working Fiddle 和一些示例数据。

    【讨论】:

      【解决方案2】:

      此示例使用 WPF,但您也可以对 Winforms 或 Webforms 使用“PivotWorkingHours”方法...

      输出:

      窗口:

      <Window x:Class="WpfApplication5.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="MainWindow" Height="350" Width="525">
          <Grid>
              <DataGrid Name="pivotTarget" ItemsSource="{Binding}"/>
          </Grid>
      </Window>
      

      实施:

      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
      
              List<Employee> demo = new List<Employee>
                                    {
                                        new Employee{Name = "Frank", WorkDays = new List<WorkDay>
                                                                              {
                                                                                  new WorkDay{Date = new DateTime(2001,1,2), Hours = 8},
                                                                                  new WorkDay{Date = new DateTime(2001,1,3), Hours = 7},
                                                                              }},
                                        new Employee{Name = "Herbert", WorkDays = new List<WorkDay>
                                                                              {
                                                                                  new WorkDay{Date = new DateTime(2001,1,2), Hours = 8},
                                                                                  new WorkDay{Date = new DateTime(2001,1,4), Hours = 7},
                                                                              }}
      
                                    };
              pivotTarget.DataContext = PivotWorkingHours(demo);
      
          }
      
          private DataTable PivotWorkingHours(IEnumerable<Employee> employees)
          {
              DataTable result = new DataTable();
              result.Columns.Add("Date", typeof(DateTime));
              foreach (string name in employees.Select(x => x.Name).Distinct())
              {
                  result.Columns.Add(name, typeof(int));
              }
              foreach (DateTime date in employees.SelectMany(e => e.WorkDays.Select(wd => wd.Date)).Distinct())
              {
                  DataRow row = result.NewRow();
                  row["Date"] = date;
                  foreach (Employee employee in employees)
                  {
                      row[employee.Name] = employee.WorkDays.Where(wd => wd.Date == date).Sum(wd => wd.Hours);
                  }
                  result.Rows.Add(row);
              }
      
      
              return result;
          }
      }
      
      class Employee
      {
          public String Name { get; set; }
          public List<WorkDay> WorkDays { get; set; }
      }
      
      class WorkDay
      {
          public DateTime Date { get; set; }
          public int Hours { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-24
        • 1970-01-01
        • 1970-01-01
        • 2021-04-14
        • 2010-12-07
        • 2012-04-11
        • 1970-01-01
        • 2023-04-04
        相关资源
        最近更新 更多