Get distinctive values ​​from list <T> in c #

Hello programmers! Actually I need different values ​​from the list after adding some values ​​to the List. My code looks like this

             List<Employee_Details> Temp = new List<Employee_Details>();
             Temp =(List<Employee_Details>) newform.Tag;


But I am directly ignoring and not adding value.

Please help me.


source to share

3 answers


prefers that both GetHashCode

and Equals

be defined for both the type being compared and to provide the supplied equality comparator. If two objects have the same hash code, then they are checked for equality.

You can implement GetHashCode

and Equals

its type, but sometimes I have found that the definition of equality in one case is not always appropriate for all cases - that is, in the case of the interface UI that is enough to verify that the two object identifier same as in the user interface there may not be all the same data defined on the object that is actually reachable (so one true equality may not always be true).

So I prefer to use the type IEqualityComparer<T>

for Employee_Details

and then provide an instance of it to the methodDistinct

public class EmployeeDetailsComparer : IEqualityComparer<Employee_Details>
    #region IEqualityComparer<int> Members
    public bool Equals(Employee_Details x, Employee_Details y)
        //define equality
      if(x == null)
        return y == null;
      else if(y == null) return false;
      //now check the fields that define equality - if it a DB record,
      //most likely an ID field
      return x.ID == y.ID; //this is just A GUESS :)

    public int GetHashCode(Employee_Details obj)
        //define how to get a hashcode
        //most obvious would be to define it on the IDs, 
        //but if equality is defined across multiple fields
        //then one technique is to XOR (^) multiple hash codes together
        //null-check first
        if(obj == null) return 0;
        //now either:
        return obj.ID.GetHashCode();
        //or something like
        return obj.FirstName.GetHashCode() ^ obj.Surname.GetHashCode();



Now you can do:

EMP_DETAILS.Concat(Temp).Distinct(new EmployeeDetailsComparer());


Although note that does not actually do anything - you need to actually capture the return value of the method Concat

as either IEnumerable<Employee_Details>

, or "realize" it to an array or a list:

Employee_Details[] result = 
  EMP_DETAILS.Concat(Temp).Distinct(new EmployeeDetailsComparer()).ToArray();


You will now replace EMP_DETAILS

with this. If so List<Employee_Details>

, you can simply do:

  EMP_DETAILS.Concat(Temp).Distinct(new EmployeeDetailsComparer()).ToList();


In reality, implementing a good multi-value hash code is tricky, but the approach ^

works in most cases. The goal is to get different hash codes for different instances Employee_Details





uses Equals

comparison of contained objects. If you have a default implementation of Equals in Employee_Details

, you are probably comparing references.

So you have options:



You need to implement IEqualityComparer<T>

in your class and provide your own methods GetHashCode

and Equals


class Employee_Details : IEqualityComparer
    public int Employee_ID;

    public Employee_Details(int empID)
        Employee_ID = empID;

    public new bool Equals(object x, object y)
        return x.ToString().Equals(y.ToString());

    public int GetHashCode(object obj)
        return obj.ToString().ToLower().GetHashCode();

    public override String ToString()
        return Employee_ID.ToString();




All Articles