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

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - Zeus

11月11日北京.NET俱乐部活动通告 DotNet入门介绍 从Reflector说起 将Excel读入DateTable的问题,兼谈考虑复用的设计 [导入]小记 [导入]北京这几天总是雷暴天气,非常反常 [导入]天路 [导入]翻开一张尘封已久的帖子 [导入]大城小事 [导入]闲人逸事 [导入]Sigh [导入]生活带给我的真正快乐,是能够助人助己 [导入]HANNIBAL [导入]近期的事情 [导入]最近比较忙 [导入]李玉平 [导入]子非鱼 [导入]Plan2 2 tips
选择DataTable中的唯一值(Distinct)
Zeus · 2006-10-31 · via 博客园 - Zeus

    开发中经常用到筛选DataTable等数据源中的唯一值(类似SQL中Distinct的返回结果),在.NET FX 1.x下我是这写的:

 1static DataTable SelectDistinct(string ReturnTableName, DataTable SourceTable, string ReturnFieldName, string AdditionalFilterExpression)
 2{
 3DataTable dt = new DataTable(ReturnTableName);
 4dt.Columns.Add(ReturnFieldName, SourceTable.Columns[ReturnFieldName].DataType);
 5object LastValue = null;
 6foreach (DataRow dr in SourceTable.Select("", ReturnFieldName))
 7{
 8if (LastValue == null || !(ColumnEqual(LastValue, dr[ReturnFieldName])))
 9{
10LastValue = dr[ReturnFieldName];
11dt.Rows.Add(new object[] { LastValue });
12}

13}

14if (ds != null)
15ds.Tables.Add(dt);
16return dt;
17}

18
19static bool ColumnEqual(object A, object B)
20{
21// Compares two values to see if they are equal. Also compares DBNULL.Value.
22// Note: If your DataTable contains object fields, then you must extend this
23// function to handle them in a meaningful way if you intend to group on them.
24
25if (A == DBNull.Value && B == DBNull.Value) // both are DBNull.Value
26return true;
27if (A == DBNull.Value || B == DBNull.Value) // only one is DBNull.Value
28return false;
29return (A.Equals(B)); // value type standard comparison
30}

31

后来这样写:

 1private DataTable SelectDistinct(DataTable sourceTable, string sourceColumn)
 2{
 3    DataTable result = null;
 4    try
 5    {
 6        result = new DataTable();
 7        result.Columns.Add(sourceColumn, sourceTable.Columns[sourceColumn].DataType);
 8        Hashtable ht = new Hashtable();
 9        foreach (DataRow dr in sourceTable.Rows)
10        {
11            if (!ht.ContainsKey(dr[sourceColumn]))
12            {
13                ht.Add(dr[sourceColumn], null);
14                DataRow newRow = result.NewRow();
15                newRow[sourceColumn] = dr[sourceColumn];
16                result.Rows.Add(newRow);
17            }

18        }

19        return result;
20    }

21    catch (System.Exception ex)
22    {
23        ExceptionManager.Publish(ex);
24        return null;
25    }

26    finally
27    {
28        if (result != null)
29            result.Dispose();
30    }

31}

32
33

再后来又这样写:

 1object[] distinctRoomType = GetDistinctValues(dt,"Roomtype");
 2
 3Here is the method definition. 
 4
 5public object[] GetDistinctValues(DataTable dtable,string colName)
 6{
 7Hashtable hTable = new Hashtable();
 8foreach(DataRow drow in dtable.Rows)
 9{
10try
11{
12hTable.Add(drow[colName],string.Empty);
13}

14catch{}
15}

16object[] objArray = new object[hTable.Keys.Count ];
17hTable.Keys.CopyTo(objArray,0);
18return objArray;
19}

20

现在.NET FX 2.0中只要一句就可以搞定了,方便了许多:

1DataTable d = dataSetName.dataTableName.DefaultView.ToTable(truenew string[] "ColumnName" });