C# 从数据表中选择不同的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15070664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting distinct records from DataTable
提问by user1985189
I've checked this question hereas well as a number of other links all proposing similar solutions, but when I go distinctTable = dt.DefaultView.ToTable(true, "FILENAME");
I get an error message saying:
我在这里检查了这个问题以及许多其他都提出了类似解决方案的链接,但是当我去时,distinctTable = dt.DefaultView.ToTable(true, "FILENAME");
我收到一条错误消息:
A field or property with the name 'LOCATION' was not found on the selected data source.
在所选数据源中找不到名为“LOCATION”的字段或属性。
Now, I have four columns altogether: Location, Folder, Filename, and Status. The values for Folder and Filename I select from the database, but the values for location and status are determined thru C# code. I have no idea why it's saying that the Location column isn't found because it works fine when I omit the above line.
现在,我总共有四列:位置、文件夹、文件名和状态。我从数据库中选择文件夹和文件名的值,但位置和状态的值是通过 C# 代码确定的。我不知道为什么它说找不到位置列,因为当我省略上面的行时它工作正常。
I don't think I can use SQL because some of the records are being generated by user input (i.e. they aren't coming from the database).
我不认为我可以使用 SQL,因为有些记录是由用户输入生成的(即它们不是来自数据库)。
I've also tried
我也试过
view = new DataView(dt);
distinctTable = view.ToTable(true, "LOCATION", "FOLDER", "FILENAME", "STATUS");
or just distinctTable = view.ToTable(true, "FILENAME");
for the latter statement but this doesn't seem to do anything - it doesn't throw an exception but it doesn't eliminate duplicate records either.
或者只是distinctTable = view.ToTable(true, "FILENAME");
对于后一个语句,但这似乎没有任何作用 - 它不会引发异常,但也不会消除重复记录。
What am I doing wrong?
我究竟做错了什么?
回答by user826840
How about using LINQ?
使用LINQ怎么样?
var items = yourdatatable.AsEnumerable().Distinct();
var items = yourdatatable.AsEnumerable().Distinct();
回答by Ann L.
It reads as if there are two issues:
读起来好像有两个问题:
- You get an error when you databind to (I assume) your derived table;
- When you try something else, you don't get an error, but you don't get the desired distinct records, either.
- 当您数据绑定到(我假设)您的派生表时,您会收到错误消息;
- 当您尝试其他操作时,您不会收到错误消息,但也不会获得所需的不同记录。
The code you post at the top:
您在顶部发布的代码:
distinctTable = dt.DefaultView.ToTable(true, "FILENAME");
... is going to give you a data table with exactly one column: FILENAME. So when you bind to it, you'll get an error if what you bind to is looking for a LOCATION column as well.
... 将为您提供一个只有一列的数据表:FILENAME。因此,当您绑定到它时,如果您绑定到的内容也在寻找 LOCATION 列,则会出现错误。
The code you post at the bottom:
您在底部发布的代码:
view = new DataView(dt);
distinctTable = view.ToTable(true, "LOCATION", "FOLDER", "FILENAME", "STATUS");
... doesn't throw an error because it has LOCATION (and other columns) as part of the table, so the control you bind to is able to find all the columns.
... 不会抛出错误,因为它具有 LOCATION(和其他列)作为表的一部分,因此您绑定到的控件能够找到所有列。
But you say it doesn't remove duplicates. I'm wondering, when you say that you want to remove duplicates, but show code where you're only specifying one column for output, whether what you want is not to filter out exact duplicates of the entire record, but filter out records that have the same FILENAME value but different values of the other columns.
但是你说它不会删除重复项。我想知道,当您说要删除重复项,但在仅指定一列用于输出的地方显示代码时,您是否想要不是过滤掉整个记录的完全重复项,而是过滤掉以下记录具有相同的 FILENAME 值,但其他列的值不同。
You can't (as far as I know) do that with DataView.ToTable
. But you can do it with LINQ:
你不能(据我所知)用DataView.ToTable
. 但是你可以用 LINQ 做到这一点:
DataTable distinctTable = dt.AsEnumerable()
.GroupBy(r=> r.Field<string>("FILENAME"))
.Select(g=>g.First())
.CopyToDataTable();