zoukankan      html  css  js  c++  java
  • Get distinct count of rows in the DataSet

    The table in the DataSet is as follows:

    Column1     Column2

    11               A

    11               B

    22               C

    33               D

    33               E

    44               F

    Distinct count of Column1 is 4 (that is 11,22,33,44), not 6. How can I get 4 ?

            //此方法将所选字段的唯一值复制到一个新的 DataTable 。 如果字段包含 NULL 值,目标表中的记录还包含 NULL 值 
            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 });
                    }
                }
                
    ////if (ds != null)
                
    ////ds.Tables.Add(dt);
                return dt;
            }
            
    static bool ColumnEqual(object A, object B)
            {           
                
    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   
            }

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

    http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx

    http://www.cnblogs.com/Fooo/archive/2009/06/30/1513696.html

  • 相关阅读:
    day10 Python 形参顺序
    为oracle中的表格增加列和删除列
    为mapcontrol中的图层设置透明度
    最大的愿望 2007-05-10
    动心 2004年后半年
    写在十年 2007-09-15 (写给L之三)
    致vi老大 2011.1
    如潮 2011.2
    自然人——女孩思绪 (2006-09-14 08:21:51)
    朋友(2003年)
  • 原文地址:https://www.cnblogs.com/emanlee/p/1597616.html
Copyright © 2011-2022 走看看