DataTable dt = new DataTable();
dt.Columns.Add("IntValue", typeof(int));
dt.Columns.Add("StringValue", typeof(string));
dt.Rows.Add(1, "1");
dt.Rows.Add(1, "1");
dt.Rows.Add(1, "1");
dt.Rows.Add(2, "2");
dt.Rows.Add(2, "2");
var x = (from r in dt.AsEnumerable()
select r["IntValue"]).Distinct().ToList();
1
使用LINQ(.NET 3.5,C#3)
var distinctNames = ( from row in DataTable.AsEnumerable()
select row.Field<string>("Name")).Distinct();
foreach (var name in distinctNames ) { Console.WriteLine(name); }
var distinctRows = (from DataRow dRow in dtInventory.Rows
select dRow["column_name"] ).Distinct();
var distinctRows = (from DataRow dRow in dtInventory.Rows
select dRow["col1"], dRow["col2"].. ).Distinct();
4
为了改进上面的答案:dataview上的ToTable函数有一个“distinct”标志 .
//This will filter all records to be distinct
dt = dt.DefaultView.ToTable(true);
3
以下作品 . 我使用.NET 3.5 SP1为我工作
// Create the list of columns
String[] szColumns = new String[data.Columns.Count];
for (int index = 0; index < data.Columns.Count; index++)
{
szColumns[index] = data.Columns[index].ColumnName;
}
// Get the distinct records
data = data.DefaultView.ToTable(true, szColumns);
class DataTableHelper
{
public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
{
DataTable dt = new DataTable(TableName);
dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
object LastValue = null;
foreach (DataRow dr in SourceTable.Select("", FieldName))
{
if ( LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) )
{
LastValue = dr[FieldName];
dt.Rows.Add(new object[]{LastValue});
}
}
return dt;
}
private bool ColumnEqual(object A, object B)
{
// Compares two values to see if they are equal. Also compares DBNULL.Value.
// Note: If your DataTable contains object fields, then you must extend this
// function to handle them in a meaningful way if you intend to group on them.
if ( A == DBNull.Value && B == DBNull.Value ) // both are DBNull.Value
return true;
if ( A == DBNull.Value || B == DBNull.Value ) // only one is DBNull.Value
return false;
return ( A.Equals(B) ); // value type standard comparison
}
}
0
string[] TobeDistinct = {"Name","City","State"};
DataTable dtDistinct = GetDistinctRecords(DTwithDuplicate, TobeDistinct);
//Following function will return Distinct records for Name, City and State column.
public static DataTable GetDistinctRecords(DataTable dt, string[] Columns)
{
DataTable dtUniqRecords = new DataTable();
dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
return dtUniqRecords;
}
18 回答
这很容易
和dt2 datatable包含column1,Column2..ColumnNth唯一数据 .
它使用eecode过滤数据表,并将taxyear结合起来视为唯一
最简单的解决方案是使用linq,然后将结果转换为DataTable
如果我记得这对asp.net 4.0 ^ Framework有效 .
单行代码将避免
DataTable
的重复行:哪里:
_2961122_中的
ToTable()
中的通过访问特定的
DataTable
,可以从DataSet
完成相同的操作:使用LINQ(.NET 3.5,C#3)
你可以这样使用:
data
是DataTable
但表现会下降 . 尝试使用以下代码:
表现; http://onerkaya.blogspot.com/2013/01/distinct-dataviewtotable-vs-linq.html
为了改进上面的答案:dataview上的ToTable函数有一个“distinct”标志 .
以下作品 . 我使用.NET 3.5 SP1为我工作
我碰巧发现了这个:http://support.microsoft.com/default.aspx?scid=kb;en-us;326176#1
在寻找类似的东西时,只有 specifically for .net 2.0
我假设OP在使用DataTable.Select()时寻找不同 . (Select()不支持distinct)
所以这是上面链接的代码:
句法:-
EX: -
sthing喜欢?
SELECT DISTINCT .... FROM表WHERE条件
http://www.felixgers.de/teaching/sql/sql_distinct.html
注意:作业问题?和上帝保佑谷歌..
http://www.google.com/search?hl=en&rlz=1C1GGLS_enJO330JO333&q=c%23+selecting+distinct+values+from+table&aq=f&oq=&aqi=