惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
量子位
腾讯CDC
C
Check Point Blog
小众软件
小众软件
IT之家
IT之家
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
博客园_首页
S
SegmentFault 最新的问题
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler

博客园 - Hermes.Liu

消息队列、AMQP和RabbitMQ 继SqlPager之后推出一款可用于前台页面的分页控件--UrlPager SqlPager2.0分页控件(使用存储过程,支持.NET2.0) SqlPager最终版[附源码和示例程序](使用存储过程进行分页) SqlPager的再次改进(带数字翻页以及翻页样式设置功能) Asp.net文件的上传与下载 [转]什么是工作分解结构(WBS)? [转]什么是CMMI? 一个继承于DropDownList的树状控件 树型DataGrid的思路 [温故知新]c#的一些基础知识 一个简单的XML的操纵类 IBM DB2数据库使用经验小结 提供一个基于.NET的加密/解密算法[转.CNSDN.com.cn] 关于showModalDialog 的常见问题 关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 常用JavaScript(二) Transact SQL备忘 Microsoft SQLHelper类概述
在DataTable中实现DataTable.Select("Distinct")功能
Hermes.Liu · 2006-12-05 · via 博客园 - Hermes.Liu

我们有时候需要对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