将DataTable中的多个重复值排序到一行 [英] Sort several duplicate values in DataTable to one row

查看:143
本文介绍了将DataTable中的多个重复值排序到一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用SqlDataAdapter和Fill-Method从SQL数据库中导入了DataTable.

I've imported a DataTable from a SQL Database using SqlDataAdapter and Fill-Method.

我的数据表如下:

Timestamp(unix time) | Value

          x          |    10
          x          |    42
          x          |   643
          y          |     5
          y          |     9
          y          |    70

...等等.该表包含很多值(超过1000个),但始终具有相同时间戳的三行.

...and so on. The table contains a lot of values (1000+) but has always three rows with the same timestamp.

现在,我希望它看起来像这样:

Now I want it to look like this:

Timestamp(unix time) | Value 1 | Value 2 | Value 3

       x             |   10    |    42   |   643
       y             |    5    |     9   |    70

我如何以这种方式对其进行排序?

How can I sort it this way?

(如果有三个以上的值,程序应该只插入找到的前三个值)

(If there are more than three values, the programm should just insert the first three values it has found)

感谢您的帮助!

推荐答案

感谢您的方法!我现在自己解决了.

Thanks for your approach! I solved it myself now.

这是我的操作方式:

var grouped = from myRow in myDataTable.AsEnumerable()
              group myRow by myRow.Field<int>("TIMESTAMP");

foreach (var timestamp in grouped)
{
    string[] myRow = new string[5];
    myRow[0] = timestamp.Key.ToString();

    int i = 1;
    foreach (var value in timestamp)
    {
      myRow[i] = value.Field<double>("VALUE").ToString();
      i++;
      if (i > 4)
          break;
    }

    mySortedTable.Rows.Add(myRow);
}

这篇关于将DataTable中的多个重复值排序到一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆