• 去除 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();    
    }
  • 相关阅读:
    php l练习(写着玩)
    位、字节、字
    ueditor上传图片时目录创建失败的问题解决方法
    tp5插入百度富文本编辑器UEditor
    PHP实现用户异地登录提醒功能的方法
    tp5知识点
    TP5语法
    微擎url
    微擎多图片上传
    微擎函数Iserializer和Iunserializer序列化函数
  • 原文地址:https://www.cnblogs.com/gunsmoke/p/2562992.html
Copyright © 2020-2023  润新知