1

我有一个有几千行的数据表,有三列,我想搜索数据表中的一个特定列以找到不同的值。我试过这个:

DataRow[] dr = datatable.Select("SELECT DISTINCT Amount ");

金额是我试图将值分组的列名。

我得到的错误是:语法错误:'DISTINCT'运算符后缺少操作数

谢谢

4

2 回答 2

4
  DataView view = new DataView(datatable);
  DataTable distinctAmount = view.ToTable(true, "Amount", "xxx","xxxx"...);

在此处输入图像描述

于 2013-08-09T15:25:07.117 回答
2

如果您想要不同的值,您可以尝试使用以下DataView.ToTable()方法:

var distinct = datatable.DefaultView.ToTable(true, new[] { "Amount" });
foreach (DataRow item in distinct.Rows)
{
    Console.WriteLine(item[0]);
}

只需添加一个示例方法来分组Amount并显示基于此分组的行(假设OtherColumn您的数据表中有另一列):

datatable.Rows
    .Cast<DataRow>()
    .GroupBy(r => r["Amount"])
    .ToList().ForEach(group =>
    {
        Console.WriteLine("Group {0} has the following elements", group.Key);
        group.ToList().ForEach(row =>
        {
            Console.WriteLine(row["OtherColumn"]);
        });
    });
于 2013-08-09T15:13:40.830 回答