zoukankan      html  css  js  c++  java
  • 去除 DataTable中重复的Row (2)

    3.  linq group by with a custom class

    Declare Custom class

    public class PortableKey
    {
        public Dictionary<string, object> keyBag { get; set; }
    
        public PortableKey(Dictionary<string, object> Keys)
        {
            this.keyBag = Keys;
        }
    
        public override bool Equals(object obj)
        {
            PortableKey other = (PortableKey)obj;
            foreach (KeyValuePair<string, object> key in keyBag)
            {
                if (other.keyBag[key.Key] != key.Value) return false;
            }
            return true;
        }
    
        public override int GetHashCode()
        {
            // hashCodes is an array of integers represented as strings. { "1", "4", etc. }
            string[] hashCodes = keyBag.Select(k => k.Value.GetHashCode().ToString()).ToArray();
            // hash is the Hash Codes all joined in a single string. "1,4,etc."
            string hash = string.Join(",", hashCodes);
            // returns a single hash code for the combined hash. 
            // Note, this is not guaranteed unique, nor is it intended to be so.
            return hash.GetHashCode();
        }    
    }

    Create a Dictionary<string, object> with keys that we interested.

    protected Dictionary<string, object> SetDictionary(DataRow row,  string[] keys)
    {
        Dictionary<string, object> item = new Dictionary<string, object>();
        foreach(string key in keys)
        {
            item[key] = row[key];
        }    
        return item;
    }

    Dedup 

    protected DataTable Dedup(DataTable dt, params string[] keys)
    {
        var columns = dt.Columns.Cast<DataColumn>();
    
        var query = from row in dt.AsEnumerable()
                    group row by new PortableKey(  SetDictionary(row, keys)  )                              
                    into g
                    select g.First();                
        return query.CopyToDataTable();    
    }
  • 相关阅读:
    Redhat MysqlReport安装配置详解
    asp.net中服务器端控件和客户端控件的交互问题
    关于弹出对话框返回值的分析
    关于父子窗口的参数传递(引用的高手的)
    呵呵!刚刚申请!
    Loadrunner教程
    性能测试常见用语
    如何删除电脑垃圾文件
    内连接和外连接
    酒桌上的规矩
  • 原文地址:https://www.cnblogs.com/gunsmoke/p/2562992.html
Copyright © 2011-2022 走看看