Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7709675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:55:18+00:00 2026-06-01T00:55:18+00:00

Hi Programmers! Actually i need the distinct values from the List after adding some

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:55:19+00:00Added an answer on June 1, 2026 at 12:55 am

    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 – 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 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Programmers, Actually I have a DataGridViewComboBoxCell in DataGridvIew and I need to change
Programmers that actually promote their products to production need an installer. (pre-emptive programming related
I have a challenge I need some input on. I am currently recruiting programmers
I'm looking for suggestions on new development system from some programmers that have more
hi programmers! I deployed my project of winforms.It goes fine but while loading the
Hi Programmers, I need to add the checkbox and label in the same cell.I
Like many programmers, I'm prone to periodic fits of inspiration wherein I will suddenly
I have multiple programmers contributing examples for javadocs and some examples contain comments formatted
Dear fellow programmers, I am working on my B&B's website www.bordemundo.com and would like
This should be something encountered by programmers often, but I never tried to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.