List(暂存)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ListTest
{
    class Program
    {
        protected static void Main(string[] args)
        {
            List<string> strs1 = new List<string> { "1", "2", "3", "1", "4" };
            List<string> strs2 = new List<string> { "1", "0", "3", "1", "4" };

            List<User> us = new List<User>();
            us.Add(new User(Guid.Empty, "1111"));
            us.Add(new User(Guid.NewGuid(), "2222"));
            us.Add(new User(Guid.NewGuid(), "2222"));
            us.Add(new User(Guid.NewGuid(), "3333"));

            List<User> us2 = new List<User>();
            us2.Add(new User(Guid.Empty, "1111"));
            us2.Add(new User(Guid.Empty, "2222"));
            us2.Add(new User(Guid.NewGuid(), "222423w5"));

            //List去除重复
            List<User> delegateList = us.Distinct(new Compare<User>(delegate(User x, User y) { if (null == x || null == y) return false; return x.UserName == y.UserName; })).ToList();
            //List去重复
            var strs = strs1.Except(strs2).ToList();
            foreach (var item in delegateList)
            {
                Console.WriteLine(item.UserName);
            }
            //List取交集
            strs = strs1.Intersect(strs2).ToList();
            //foreach (var item in strs)
            //{
            //    Console.WriteLine(item);
            //}
            //List取交集
            List<User> delegateList2 = us.Intersect(us2, new Compare<User>(delegate(User x, User y) { if (null == x || null == y) return false; return x.UserName == y.UserName; })).ToList();
            //foreach (var item in delegateList2)
            //{
            //    Console.WriteLine(item.UserName);
            //}

            

            Console.ReadLine();
        }
    }

    public class User
    {
        public User() { }
        public User(Guid uid, string uname)
        {
            UserId = uid;
            UserName = uname;
        }
        public Guid UserId { get; set; }
        public String UserName { get; set; }
    }

    public delegate bool EqualsComparer<T>(T x, T y);
    public class Compare<T> : IEqualityComparer<T>
    {
        private EqualsComparer<T> _equalsComparer;

        public Compare(EqualsComparer<T> equalsComparer)
        {
            this._equalsComparer = equalsComparer;
        }
        public bool Equals(T x, T y)
        {
            if (null != this._equalsComparer)
                return this._equalsComparer(x, y);
            else
                return false;
        }
        public int GetHashCode(T obj)
        { return obj.ToString().GetHashCode(); }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值