【问题标题】:Use LINQ to group by multiple columns and read values of ungrouped columns使用 LINQ 按多列分组并读取未分组列的值
【发布时间】:2010-08-23 15:45:35
【问题描述】:

我需要在多个列上对数据表进行分组,并读取未分组列的数据

col1 col2 col3 col4 col5 col6 col7 col8 .. col16

在这里,我将它分组在 col1、col2 ... col6 上。但是在读取 col7 ... col16 的值时遇到问题

var groupedRows = from myTable in table.AsEnumerable()
                  group myTable by new
                  {
                      Col1 = myTable["Col1"],
                      Col2 = myTable["Col2"],
                      Col3 = myTable["Col3"],
                      Col4 = myTable["Col4"],
                      Col5 = myTable["Col5"],
                      Col6 = myTable["Col6"]
                  }
                  into groupedTable
                  select new
                  {
                      x = groupedTable.Key,
                      y = groupedTable
                  };

我遇到问题的代码是

foreach (var row in groupedRows)
{
    record = new Record();
    record.col1 = row.x.col1.ToString();
    record.col2 = row.x.col2.ToString();
    record.col3 = row.x.col3.ToString();
    record.col4 = row.x.col4.ToString();
    record.col5 = row.x.col5.ToString();
    record.col6 = row.x.col6.ToString();

    var groupdata = groupedRows.Select(x => x.y);
    if (groupdata.Count() > 0)
    {
        var groupdatas = groupdata.First();
        foreach (var item in groupdatas)
        {
        }
    }
}

我正在尝试读取未分组列的var groupdata = groupedRows.Select(x => x.y); 数据但没有用。

我在从 y 读取数据时遇到问题。 谁能推荐我如何阅读?

【问题讨论】:

  • 您应该准确指出您面临的问题。您是否需要一次只对某些列进行分组?还是有其他问题?

标签: .net linq group-by


【解决方案1】:

在此之后,您将得到一个列表列表:即一个组列表,每个组都有一个属于该组的行列表。您的 groupedTable 变量将是该组中的行列表。

这取决于您实际上要分配给 y 的内容,但如果您理解这一点,希望您会走在正确的轨道上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多