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();    
    }
  • 相关阅读:
    B00009 C语言分割字符串库函数strtok
    B00009 C语言分割字符串库函数strtok
    I00026 计算数根
    I00026 计算数根
    I00025 寻找循环数
    Magic Stones CodeForces
    Continued Fractions CodeForces
    AtCoder Beginner Contest 116 D
    Applese 的毒气炸弹 G 牛客寒假算法基础集训营4(图论+最小生成树)
    Choosing The Commander CodeForces
  • 原文地址:https://www.cnblogs.com/gunsmoke/p/2562992.html
Copyright © 2011-2022 走看看