dataView = new DataView(table);把table 转成dataView我想把table表里 id 相同的行 DISTINCT 排除直留 一条 我该怎样实现???dataView.RowFilter = ???
table id 名称 ....
----------------
1 大米 ....
2 小米 ....
3 玉米 .....
2 小米 .....
3 玉米 .....
要结果
dataView id 名称 ....
----------------
1 大米 ....
2 小米 ....
3 玉米 .....不知道问题所清楚了么? 请教
dt.Select().Distinct(c=>c.字段);
这个是LINQ,中间一个变量而已,你可以随便写成其他的什么,都可以。
怎么个随便??
调用事件里写
DataTable dt = (System.Data.DataTable)this.ViewState["Mydt"];
string[] fileds = { "Filed1", "Filed2" };//DISTINCT字段数组
DataTable newdt = this.SelectDistinct(dt, fileds);//返回过滤后的DataTable
private DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames) //4.9+
{
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 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 DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
{
foreach (string field in fieldNames)
newRow[field] = sourceRow[field]; return newRow;
}
private void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
{
for (int i = 0; i < fieldNames.Length; i++)
lastValues[i] = sourceRow[fieldNames[i]];
}
dt.Select().Distinct(cc=>cc.字段);dt.Select().Distinct(xx=>xx.字段);dt.Select().Distinct(abc=>abc.字段);dt.Select().Distinct(uu=>uu.字段);dt.Select().Distinct(a=>a.字段);
....
....
...明白了吗?
一句话的事儿:
DataTable dt = new DataTable();
DataView dv = dt.DefaultView.ToTable(true, "id").DefaultView;
非得要我写出
DataView dv = dt.DefaultView.ToTable(true, "id", "名称").DefaultView;
如果不要就是上面那样,如果要要么循环要么Linq
不过这个没有聚集功能,如果楼主要更多字段,就只能写循环了
if (数据.Count() == 0) return;//递归终止,不包含子节点时
DataTable.Select().Distinct()
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<int> distinctAges = ages.Distinct();
如下已解决
string[] 并列 = new string[] { 列名, 子列名 };//重组表也可全部
DataRow[] 数据 = 数据表.DefaultView.ToTable(true, 并列).Select(列名 + "='" + 父节数据 + "'");//子节点(获取不重复记录)
http://blog.csdn.net/xianfajushi/article/details/7550084