在DataTable中实现DataTable.Select("Distinct")功能

我们有时候需要对DataTable中数据进行Distinct处理,过滤掉重复的数据,本文给出了解决方法:

事例代码来源于:Erik Porter's Blog   Select DISTINCT on DataTable http://weblogs.asp.net/eporter/archive/2005/02/10/370548.aspx 以及 HOWTOVisualC # .NET 中实现 DataSet SELECTDISTINCT Helper 类http://support.microsoft.com/?id=326176
 
/// <summary>
        
/// 返回执行Select distinct后的DataTable
        
/// </summary>
        
/// <param name="SourceTable">源数据表</param>
        
/// <param name="FieldNames">字段集</param>
        
/// <returns></returns>

        private 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 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 DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
        
{
            
foreach (string field in fieldNames)
                newRow[field] 
= sourceRow[field];

            
return newRow;
        }


        
private void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
        
{
            
for (int i = 0; i < fieldNames.Length; i++)
                lastValues[i] 
= sourceRow[fieldNames[i]];
        }
 
使用:
DataTable dt=(System.Data.DataTable)this.ViewState["Mydt"];
            
string[] fileds={"Filed1","Filed2"};//DISTINCT字段数组
            DataTable newdt=this.SelectDistinct(dt,fileds);//返回过滤后的DataTable

posted @ 2006-12-05 23:25 缘易姿姿 阅读(4465) 评论(3)  编辑 收藏 网摘 所属分类: Asp.net(C#)C#学习资料

  回复  引用    
#1楼 2008-04-28 20:10 | jiero [未注册用户]
DataTable newTable;
newTable = SourceTable.DefaultView.ToTable(true, FieldNames);

return newTable;
  回复  引用    
#2楼 2008-08-18 11:08 | olala [未注册用户]
楼主多谢啦,

哈哈,我拷贝过来就可以用了.
  回复  引用    
#3楼 2008-11-11 16:06 | SYSPING [未注册用户]
DataTable d = dataSetName.dataTableName.DefaultView.ToTable(true, new string[] { "ColumnName" });




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-10-26 00:20 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

相关文章:

相关链接: