在DataTable上应用SELECT DISTINCT

    ADO.NET 1.x对于DataTable虽然公开了Select方法,但是未能提供如SELECT DISTINCT的方法,而这样的功能往往在实际项目中是需要的。
    下面是常用的几种方法:
    方法一:(参见 http://support.microsoft.com/kb/326176/zh-cn
   
Code
1. Add the following Private method to the class definition. This method is the same as the method that is used in other DataSetHelper articles. It is used to compare field values (including NULL).private bool ColumnEqual(object A, object B)
{
    
        
// Compares two values to see if they are equal. Also compares DBNULL.Value.
        
// Note: If your DataTable contains object fields, then you must extend this
        
// function to handle them in a meaningful way if you intend to group on them.
            
        
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
}

                    
 
2. Add the following Public method to the class definition. This method copies unique values of the field that you select into a new DataTable. If the field contains NULL values, a record in the destination table will also contain NULL values.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;
}
                   
 

    方法二:(参考 http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx
   
Code
private static DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
{
     
object[] lastValues;
     DataTable newTable;
     DataRow[] orderedRows;

     
if (FieldNames == null || FieldNames.Length == 0)
          
throw new ArgumentNullException("FieldNames");

     lastValues 
= new object[FieldNames.Length];
     newTable 
= new DataTable();

     
foreach (string fieldName in FieldNames)
          newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

     orderedRows 
= SourceTable.Select(""string.Join("", FieldNames));

     
foreach (DataRow row in orderedRows)
     
{
          
if (!fieldValuesAreEqual(lastValues, row, FieldNames))
          
{
               newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

               setLastValues(lastValues, row, FieldNames);
          }

     }


     
return newTable;
}


private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
{
     
bool areEqual = true;

     
for (int i = 0; i < fieldNames.Length; i++)
     
{
          
if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
          
{
               areEqual 
= false;
               
break;
          }

     }


     
return areEqual;
}


private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
{
     
foreach (string field in fieldNames)
          newRow[field] 
= sourceRow[field];

     
return newRow;
}


private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
{
     
for (int i = 0; i < fieldNames.Length; i++)
          lastValues[i] 
= sourceRow[fieldNames[i]];
}
 

    ADO.NET 2.0 虽然仍然未提供对DISTINCT的支持,但DataView提供了几个重载版本的ToDataTable,可以简单对DISTINCT支持:
   
Code
C# 
public DataTable ToTable (
    
bool distinct,
    
params string[] columnNames
)
 
public DataTable ToTable (
    
string tableName,
    
bool distinct,
    
params string[] columnNames
)

参数
tableName
返回的 DataTable 的名称。

distinct
如果为 
true,则返回的 DataTable 将包含所有列都具有不同值的行。默认值为 false

columnNames
一个字符串数组,包含要包括在返回的 DataTable 中的列名的列表。DataTable 包含指定的列,其顺序与这些列在该数组中的顺序相同。

返回值
一个新的 DataTable 实例,其中包含所请求的行和列。 


    在微软.NET 3.0中,LINQ提供了对复杂查询的支持,有待学习、体验。


转载于:https://www.cnblogs.com/mywebname/archive/2007/11/19/964743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值