首页 > 其他分享 >Get distinct count of rows in the DataSet

Get distinct count of rows in the DataSet

时间:2023-11-16 14:07:21浏览次数:26  
标签:count LastValue rows distinct Value FieldName dt DataTable DBNull

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




标签:count,LastValue,rows,distinct,Value,FieldName,dt,DataTable,DBNull
From: https://blog.51cto.com/emanlee/8418769

相关文章

  • [题解] CF1748E Yet Another Array Counting Problem
    YetAnotherArrayCountingProblem给你一个长度为\(n\)的序列和一个数\(m\),求有多少个长度为\(n\)的序列\(b\)满足:\(\foralli\in[1,n],b_i\in[1,m]\)。对于每个区间\([l,r]\),\(b\)中最大值最靠左的位置和\(a\)相同。\(n,m\le2\times10^5,n\ti......
  • 开源项目SourceBrowser 功能实现中问题修复
    前段时间看到在线原源码浏览网站SourceBrowser,就好奇怎么读的代码展示的,就拔下源码看了下,然后自己打算简单实现下,不想每个工作日弄个把小时弄了两周,才解决报错问题,可以读取到文档,也简单学习了下Roslyc.原项目中时net472跑,我先直接copy拿段读取代码,新建一个控......
  • D. Counting Factorizations
    D.CountingFactorizationsTheprimefactorizationofapositiveinteger$m$istheuniquewaytowriteitas$\displaystylem=p_1^{e_1}\cdotp_2^{e_2}\cdot\ldots\cdotp_k^{e_k}$,where$p_1,p_2,\ldots,p_k$areprimenumbers,$p_1<p_2<......
  • 使用PageHelper.startPage时 net.sf.jsqlparser.parser.ParseException: Encountered
    使用PageHelper.startPage时net.sf.jsqlparser.parser.ParseException:Encountered解决方案对比代码:原来的写法:PageHelper.startPage(page,size,order);List<xxx>list=xxxMapperExt.selectxxx(id,type);修改之后:PageHelper.startPage(page,size);List<xxx>list=xxxM......
  • 入门级throw与throws的区别
    throw与throws区别一:    throw与throws的区别二:    throw与throws的区别三:   throw与throws的区别四:throw,如果执行了,那么一定是抛出了某种异常了,安生throws表示可能出现,但不一定。......
  • 异常处理机制之throw与throws的区别是什么?(新手必看)
    ......
  • 异常处理机制(一)之throw与throws的区别是什么?
    throw与throws的区别一、throw:throw:抛出异常throw:代表动作,表示抛出一个异常的动作;throw:方法体内,可以作为单独语句使用throw:只能抛出一个异常对象throw是语句抛出一个异常,一般是在代码块的内部,当程序出现某种逻辑错误时由程序员主动抛出某种特定类型的异常1.1声明格式: ......
  • throw与throws的区别
    throw和throws都与异常处理有关,但它们的用法和含义不同。两者的区别throw在方法体内使用,throws在方法声明上使用。throw后面接的是异常对象,只能接一个。throws后面接的是异常类型,可以接多个,多个异常类型用逗号隔开。throw是在方法中出现不正确情况时,手动来抛出异......
  • 剖析网络测量:Counting and Measuring Network Traffic
    全文共18000字,讲解了网络测量和计数中的多方面知识:网络测量的意义、网络测量的手段分类、网络测量在实现上的挑战、以及解决这些挑战所用到的技术和协同方案等等。参考书籍有:《NetworkAlgorithmics:AnInterdisciplinaryApproachtoDesigningFastNetworkedDevices(2ndE......
  • FastReport打印DataBand分列:DataBand.Columns.Count
    FastReport打印DataBand分列,DataBand.Columns.Count。看图,转载请注明海宏软件:下面的图片:diffImg、pltImg、rbcImg实际上是三行记录,横着打印了。 C#下载网页文件并存入DataTable的DataRow的DataColumn字段里:if(web==null)web=newWebClient();row["oImg"]=web.Down......