Hi Programmers!
Actually i need the distinct values from the List after adding some values to List.
My code goes like this
List<Employee_Details> Temp = new List<Employee_Details>();
Temp =(List<Employee_Details>) newform.Tag;
EMP_DETAILS.Concat(Temp).Distinct();
But i directly ignore and do not add the values.
Please help me out.
Distinctprefers bothGetHashCodeandEqualsto be defined either for the type being compared, or to have an equality comparer supplied. If two objects have the same hash code, then they are checked for equality.You can implement
GetHashCodeandEqualsfor your type – but I sometimes find that a definition of equality in one case isn’t always suitable for all cases – i.e. in UI cases it might be enough only to check that two objects’ IDs match, because the UI might not have all the same data defined on the object that is actually available (therefore a single true equality can’t always be satisfied).So I prefer to implement the
IEqualityComparer<T>type forEmployee_Detailsand then supply an instance of it to theDistinctmethodNow you can do:
Although note that doesn’t actually do anything – you need to actually capture the return value of the
Concatmethod, either as anIEnumerable<Employee_Details>, or ‘realise’ it into an array or list:Now you replace
EMP_DETAILSwith that. If it’s aList<Employee_Details>you can just do:In reality implementing a good hashcode across multiple values is tricky – but the
^approach works okay in most cases. The goal is to ensure that you get different hashcodes for differentEmployee_Detailsinstances.