C#- 泛型去除重复项

  今天被这个问题纠结了好一会。如何去除重复项,我遇到的问题是,在判断是否重复的条件是有两个,一个信息来源,一个是信息标题。

  最后使用了哈希后很好的解决,感觉挺高效的。代码贴下,做一个备忘

  

        //防止群发,出现重复通知,去除重复项
        private List<UserEmail> GetNotRepeatSentingEmail(List<UserEmail> LSentingEmail)
        {
            List<UserEmail> Result = new List<UserEmail>();
            Hashtable hash = new Hashtable();
            Result.Clear();
            hash.Clear();
            for (int i = 0; i < LSentingEmail.Count; i++)
            {
                if (!hash.ContainsKey(LSentingEmail[i].T_To) && !hash.ContainsValue(LSentingEmail[i].T_Subject))
                {
                    hash.Add(LSentingEmail[i].T_To, LSentingEmail[i].T_Subject);
                    Result.Add(LSentingEmail[i]);
                }
            }
            return Result;
        }

 

  实际,这种做法不正确,

  经实验得再改进成如下:

 

        //防止群发,出现重复通知,去除重复项
        private List<UserEmail> GetNotRepeatSentingEmail(List<UserEmail> LSentingEmail)
        {
            List<UserEmail> Result = new List<UserEmail>();
            Hashtable hash = new Hashtable();
            Result.Clear();
            hash.Clear();
            for (int i = 0; i < LSentingEmail.Count; i++)
            {
                string strKeys = LSentingEmail[i].T_To + "|" + LSentingEmail[i].T_Subject;
                if (!hash.ContainsKey(strKeys ))
                {
                    hash.Add(strKeys,"");
                    Result.Add(LSentingEmail[i]);
                }
            }
            return Result;
        }

 

转载于:https://www.cnblogs.com/cxeye/p/3581906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值