载数据库表中,用select distinct [列名]……很容易实现返回独有的列值,那么在内存中的DataTable对像如何实现类似的功能?

解决方案 »

  1.   

    用一个循环,生成另一个DataTable
      

  2.   

    不可以的,要自己写一个方法的
    public DataRow[] GetDistinctRow(DataTable aTable,string[] columnList)
    {
    DataRow[] rowList = null;
    if ( aTable.Count <= 0)
                return rowList;
    string strOrginSort = aTable.Sort ;
    string strSort = "";
    for (int i = 0 ; i < columnList.Length ; i++)
                  strSort += " ," + columnList[i];
    if ( strSort.Trim().Length > 0)
                 strSort = strSort.Remove(0,2);
    aTable.Sort  = strSort;
    string strColValue = "";
    string strFlag = "-1";
    DataRow aRow = aTable[0].Row ;
    for ( int iCol = 0 ; iCol < columnList.Length ; iCol++)
                strColValue += aRow[columnList[iCol]] .ToString().Trim();
    if (strFlag == strColValue)
                 strFlag = "-2";
    ArrayList array = new ArrayList();
    for(int iRow = 0;iRow< this.Count;iRow++)
    {
                aRow = aTable[iRow].Row;
                strColValue = "";
                for ( int iCol = 0 ; iCol < columnList.Length ; iCol++)
             strColValue += aRow[columnList[iCol]].ToString().Trim();
               if ( strFlag != strColValue)
               {
        array.Add(aRow);
        strFlag = strColValue ;
                }
    }
    rowList = new DataRow[array.Count];
    for ( int iRow = 0 ; iRow < rowList.Length ; iRow++)
                rowList[iRow] = (DataRow)array[iRow];
    return rowList;
    }
      

  3.   

    DataTable是矩阵形式存储在内在中:DataTable.Row[i][j]:表示i行j列的数据.for(int i = 0 ; i<DataTable.Row.Count; i++)
      for(int j=0; j< DataTable.Columns.Count;j++)
      {
          string Value = DataTable[i][j];  //Value表示当前矩阵中一个元素值.
      }
      

  4.   

    http://jiezhi.cnblogs.com/archive/2005/01/05/86838.html
      

  5.   

    自己在common类中定义这样的方法吧
    我在1.0的时候就在寻找,最后还是用自己写的方法 :)