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

  • SEARCH
  • Home
  • 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 6871035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:45:23+00:00 2026-05-27T03:45:23+00:00

So I understand how to remove duplicates in a list when it comes to

  • 0

So I understand how to remove duplicates in a list when it comes to strings and int, etc by using Distinct() from Linq. But how do you remove duplicates based on a specific attribute of an object?

For example, I have a TimeMetric class. This TimeMetric class has two attributes: MetricText and MetricTime. I have a list of TimeMetrics called MetricList. I want to remove any duplicates TimeMetric with the same MetricText attribute. The TimeMetric value can be the same but if any TimeMetric has the same MetricText, it must be unduplicated.

  • 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-05-27T03:45:23+00:00Added an answer on May 27, 2026 at 3:45 am

    You need to use the second overload of Distinct that takes an IEqualityComparer<TimeMetric> instance as a second parameter. Define a comparer like this:

    class MyComparer : IEqualityComparer<TimeMetric>
    {
        public bool Equals(TimeMetric x, TimeMetric y)
        {
            return x.MetricText.Equals(y.MetricText);
        }
    
        public int GetHashCode(TimeMetric obj)
        {
            return obj.MetricText.GetHashCode();
        }
    }
    

    Important note: The above code does not check for the case where the MetricText property is null (and it sounds like it could be, since it’s most probably a string). You should do that and return 0 from GetHashCode if MetricText is null. On the other hand, if the type of MetricText is a value type, you don’t need to perform any modification.

    And then:

    var list = new List<TimeMetric> { ... };
    var unique = list.Distinct(new MyComparer());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't understand why the fadeOut works but the remove doesn't. I found out
I understand that const_cast to remove constness of objects is bad, I have the
I understand what System.WeakReference does, but what I can't seem to grasp is a
I understand the main function of the lock key word from MSDN lock Statement
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
Possible Duplicate: XmlSerializer: remove unnecessary xsi and xsd namespaces I'm generating some XML using
Which kind of algorithm does Neatimage use to remove noise and grain from photos?
I understand that collections like the Hashtable are synchronized, but can someone explain to
Could someone help me understand whats going on here, Ive just started using Jquery
I would like to remove a broken symlink in a directory using Perl. In

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.