dataView = new DataView(table);把table 转成dataView我想把table表里 id 相同的行 DISTINCT 排除直留 一条 我该怎样实现???dataView.RowFilter = ???
 table   id    名称  ....
----------------
   1    大米  ....
   2    小米  ....
   3    玉米  .....
   2    小米  .....
   3    玉米  .....
要结果 
  dataView   id    名称  ....
----------------
   1    大米  ....
   2    小米  ....
   3    玉米  .....不知道问题所清楚了么?  请教
   

解决方案 »

  1.   

    用select语句查询一下  把结果填充进去就行了
      

  2.   

     DataTable dt = new DataTable();
                dt.Select().Distinct(c=>c.字段);
      

  3.   

    table 是根据需求分阶段添丛进去的 就要上面那结果
      

  4.   

    C??
    这个是LINQ,中间一个变量而已,你可以随便写成其他的什么,都可以。
      

  5.   

     
    怎么个随便?? 
     调用事件里写
    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]];
            }
      

  6.   

    http://support.microsoft.com/default.aspx?scid=kb;en-us;326176#1
      

  7.   

    怎么个随便??  ----------------DataTable dt = new DataTable();
    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.字段);
    ....
    ....
    ...明白了吗?
      

  8.   

    我靠,大家用这么麻烦吗?
    一句话的事儿:
              DataTable dt = new DataTable();
             DataView dv = dt.DefaultView.ToTable(true, "id").DefaultView;
      

  9.   

    还要我说多清楚?
    非得要我写出
    DataView dv = dt.DefaultView.ToTable(true, "id", "名称").DefaultView;
      

  10.   

    我不清楚楼主的“……”是什么意思,是说还要其它字段吗?如果要的话,那怎么个聚集方法?
    如果不要就是上面那样,如果要要么循环要么Linq
      

  11.   

    soga  起来看了眼参数   很对
      

  12.   

    嘿嘿,我不会使LINQ,也受教了
    不过这个没有聚集功能,如果楼主要更多字段,就只能写循环了
      

  13.   

    IEnumerable<DataRow> 数据 = 数据表.Select(列名 + "='" + 父节数据 + "'").Distinct();//子节点
    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