C# ASP.NET:从数据表中过滤唯一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1276150/
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
ASP.NET : Filter unique rows from Data table
提问by Hemant Kothiyal
Hi i am using data table in asp.net. I am not able to get how can we filter unique data from data table .
嗨,我在asp.net 中使用数据表。我不知道如何从数据表中过滤唯一数据。
Problem is describe below
问题描述如下
Data Table:
Consumer No Date Value
ABC001 1st Aug 09 1
ABC001 1st Aug 09 2
ABC001 2nd Aug 09 1
XYZ001 1st Aug 09 1
XYZ002 1st Aug 09 1
XYZ002 1st Aug 09 2
I would like following output based upon filter applied over first and second column. In output we can see that there is unique combination of first and second column.
我想要基于应用于第一列和第二列的过滤器的以下输出。在输出中,我们可以看到第一列和第二列的独特组合。
Consumer No Date
ABC001 1st Aug 09
ABC001 2nd Aug 09
XYZ001 1st Aug 09
XYZ002 1st Aug 09
How can i apply filter on data table?
如何在数据表上应用过滤器?
采纳答案by rahul
yourdataset.Tables["TableName"].DefaultView.ToTable(true,"disticecolumn");
Creates and returns a new DataTable based on rows in an existing DataView.
基于现有 DataView 中的行创建并返回一个新的 DataTable。
true in the .ToTable method specifies that returned DataTable contains rows that have distinct values for all its columns.
.ToTable 方法中的 true 指定返回的 DataTable 包含所有列都具有不同值的行。
From msdnarticle
来自msdn文章
Edit:
编辑:
It would be easier to do this from a database where you can use the 'distinct' keyword.
从可以使用“ distinct”关键字的数据库中执行此操作会更容易。
回答by ala
private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
{
object[] lastValues;
DataTable newTable;
DataRow[] orderedRows;
if (FieldNames == null || FieldNames.Length == 0)
throw new ArgumentNullException("FieldNames");
lastValues = new object[FieldNames.Length];
newTable = new DataTable();
foreach (string fieldName in FieldNames)
newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);
orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));
foreach (DataRow row in orderedRows)
{
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
{
newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));
setLastValues(lastValues, row, FieldNames);
}
}
return newTable;
}
private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
{
bool areEqual = true;
for (int i = 0; i < fieldNames.Length; i++)
{
if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
{
areEqual = false;
break;
}
}
return areEqual;
}
private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
{
foreach (string field in fieldNames)
newRow[field] = sourceRow[field];
return newRow;
}
private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
{
for (int i = 0; i < fieldNames.Length; i++)
lastValues[i] = sourceRow[fieldNames[i]];
}
source: http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx
来源:http: //weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx
回答by Preets
There is apparently no way you can apply a DISTINCT filter on the Select() method of the DataTable.
显然您无法在 DataTable 的 Select() 方法上应用 DISTINCT 过滤器。
However, you can create a new DataTable using the following line of code:
但是,您可以使用以下代码行创建新的 DataTable:
DataTable filterTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");
This should return distinct rows, that meet your requirement!
这应该返回满足您要求的不同行!
Credit to DevPinoy.org!
感谢DevPinoy.org!
回答by Awais
You can use LINQ to do something like this :
您可以使用 LINQ 执行以下操作:
var resultSet = (from r in dt.AsEnumerable()
select r["ConsumerNo"]).Distinct().ToList();
see the post Using LINQ to manipulate DataTable's data
请参阅使用 LINQ 来操作 DataTable 的数据的帖子
best Rgds
最佳 Rgds
回答by user2695564
DataTable filteredTable = yourDataTable.DefaultView.ToTable("TargetTable", true, "Consumer", "No", "Date");
this code fulfilled my requirement....Thanks! A lot!
这段代码满足了我的要求......谢谢!很多!