微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐