zoukankan      html  css  js  c++  java
  • DataTable,List去重复记录的方法(转载)

    DataTable,List去重复记录的方法

    思路:将DataTable转成IEnumerable,然后就能调用Distinct方法了

    转载自:菩提树下的杨过 http://yjmyzz.cnblogs.com/
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Data; 
    using System; 

    namespace ConsoleApplication2 

        
    class Program 
        { 
            
    static void Main(string[] args) 
            { 
                DataTable tbl 
    = new DataTable(); 
                tbl.Columns.Add(
    "Id"typeof(System.Int32)); 
                tbl.Columns.Add(
    "City"typeof(System.String)); 
                tbl.Columns.Add(
    "Province"typeof(System.String)); 

                tbl.Rows.Add(
    1"武汉""湖北"); 
                tbl.Rows.Add(
    2"应城""湖北"); 
                tbl.Rows.Add(
    3"武汉""湖北"); 

                IEnumerable 
    <DataRow> r = tbl.AsEnumerable().Distinct(new CityComparer()); 
              
              

                
    //到这一步,r里就是去重复的记录了 

                
    foreach (var item in r) 
                { 
                    Console.WriteLine(item[
    "Id"+ "" + item["City"+ "" + item["Province"]); 
                } 

                Console.ReadLine(); 
            } 


            
        } 

        
    class CityComparer : IEqualityComparer <DataRow> 
        { 
            
    public bool Equals(DataRow r1, DataRow r2) 
            { 
                
    return r1["City"== r2["City"]; 
            } 

            
    public int GetHashCode(DataRow obj) 
            { 
                
    return obj.ToString().GetHashCode(); 
            } 


        } 

  • 相关阅读:
    java-日期转换
    java-Timestamp
    java-判断年份是不是闰年
    Java中Date与String的相互转换
    ORA-01830
    js数组合并
    js清空子节点
    私钥密码
    图片基本样式
    XMLHttpRequest: 网络错误 0x2ee4, 由于出现错误 00002ee4 而导致此项操作无法完成
  • 原文地址:https://www.cnblogs.com/atravellers/p/1649884.html
Copyright © 2011-2022 走看看