1. 程式人生 > >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();
        }
    }