简体   繁体   English

C#与DataTable中的所有区别值

[英]c# all distincts value from datatable

I've found this piece of code that can be used to get all distinct values. 我发现这段代码可用于获取所有不同的值。 But my datatable has 10 columns. 但是我的数据表有10列。 The distinctValues only shows the columns I write in the toTable(); uniqueValues仅显示我在toTable()中编写的列; Is it possible to use this function, but also show the rest of the columns? 是否可以使用此功能,但还要显示其余的列?

DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2");

Unless those columns you mention are the full key to the table, there is no guarantee that for a particular combination of those two columns the other columns will have exactly one value. 除非您提到的那些列是表的完整键,否则不能保证对于这两个列的特定组合,其他列将仅具有一个值。

And if they were the key, then there would be no need to use a "distinct" filter. 如果它们关键,那么就无需使用“独特”的过滤器。

You can use Linq-To-DataTable 您可以使用Linq-To-DataTable

var distinct = from row in table.AsEnumerable()
               group row by new
               {
                   Col1 = row.Field<string>("Column1"),
                   Col2 = row.Field<string>("Column2")
               } into Group
               select Group.First()
DataTable tblDistinct = distinctRows.CopyToDataTable();

(assuming that you just want an arbitrary row[the first]) (假设您只想要任意行[第一个])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM