zoukankan      html  css  js  c++  java
  • Asp.Net : 实现一个 DataSet 或DataTable SELECT DISTINCT (字段唯一性)

            //此方法将所选字段的唯一值复制到一个新的 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   
            }
  • 相关阅读:
    均匀采样单位圆
    3Sum
    查看SQL语句在SQL Server上的执行时间
    ASP.NET页面请求处理
    原型模式
    ASP.NET页面错误处理
    电子商务推荐位商品模型设计
    HttpModule与HttpHandler使用
    装饰者模式
    ASP.NET编程模型
  • 原文地址:https://www.cnblogs.com/Fooo/p/1513696.html
Copyright © 2011-2022 走看看