Wednesday, December 10, 2014

find the different between 2 list

//DELETE
var oldDifferentList = oldSchool.Students.Except(newSchool.Students, new StudentComparer()).ToList();

//ADD 
var newDifferentList = newSchool.Students.Except(oldSchool.Students, new StudentComparer ()).ToList();

//EDIT

var statusChangedList = oldSchool.Students.Union(newSchool.Students).GroupBy(x => x.StudentID).Where(y => y.First().Status!= y.Last().Status).ToList();



//COMPARER
public class StudentComparer : IEqualityComparer<Student>
{
        public bool Equals(Student p1, Student p2)
        {
            if (Object.ReferenceEquals(p1, p2))
                return true;

            return p1.StudentID == p2.StudentID && p1.City == p2.City;
        }

        public int GetHashCode(Student obj)
        {
            if (Object.ReferenceEquals(obj, null)) return 0;

            int hashStudentID = obj.StudentID.GetHashCode();
            int hashCity = obj.City.GetHashCode();


            return hashStudentID ^ hashCity;
        }
}

No comments:

Post a Comment