简体   繁体   中英

Get Distinct values from List<T> in c#

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.

Distinct prefers both GetHashCode and Equals to 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 GetHashCode and Equals for your type - but I sometimes find that a definition of equality in one case isn't always suitable for all cases - ie 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 for Employee_Details and then supply an instance of it to the Distinct method

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's 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();
    }

    #endregion
}

Now you can do:

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

Although note that doesn't actually do anything - you need to actually capture the return value of the Concat method, either as an IEnumerable<Employee_Details> , or 'realise' it into an array or list:

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

Now you replace EMP_DETAILS with that. If it's a List<Employee_Details> you can just do:

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

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 different Employee_Details instances.

Distinct method uses Equals comparison of contained objects. If you have default implementation of Equals in Employee_Details then you probably compare references.

So you have to options:

  1. Implement Equals method for your Employee_Details
  2. Use overload of Distinct method that accepts IEqualityComparer

You need to implement IEqualityComparer<T> in your class and provide your own GetHashCode and Equals methods.

http://msdn.microsoft.com/en-us/library/ms132040.aspx

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM