首页 > 代码库 > LINQ获取两个List的交集

LINQ获取两个List的交集

1.调用:

UserList = UserList.ToList().Intersect(userIDList, new MyUserComparer()).AsQueryable();

2.需要重写的方法:

public class MyUserComparer : IEqualityComparer<MyUser>
    {
        public bool Equals(MyUser x, MyUser y)
        {
            //throw new NotImplementedException();
            return x.UserID == y.UserID;
        }
        public int GetHashCode(MyUser obj)
        {
            return obj.ToString().GetHashCode();
        }
    }


LINQ获取两个List的交集