พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
ASP, ASP.NET Articles / DataTable.Select() distinct
Administrator
Fri 2 Jun 2006 21:38:56
รอจน .Net 2.0 ฟังก์ชั่น Select() ก็ยังไม่รองรับ Distinct ซึ่งจะช่วยให้การทำงานเร็วขึ้น แต่เมื่อไม่มีก็สามารถสร้างขึ้นเองได้ครับ

using System.Data;
using System.Collection;

public DataTable SelectDistinct(DataTable dt, string keyfield)
{
    DataTable newTable = dt.Clone();
    ArrayList list = new ArrayList();
    if(dt != null && dt.Rows.Count > 0){
       foreach(DataRow r in dt.Rows){
          if(list.Count > 0 && !list.Contains(r[keyfield])){
             newTable.ImportRow(r);
             list.Add(r[keyfield]);
          }else if(list.Count == 0){
             newTable.ImportRow(r);
             list.Add(r[keyfield]);
          }
       }
    }else{
       newTable = dt.Clone();
    }
    return newTable;
}

เท่านี้เราก็สามารถ Select distinct จาก DataTable ได้แล้วครับ
asdf
asdf
Fri 8 Jun 2007 18:27:28
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login