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 8256041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:43:37+00:00 2026-06-08T01:43:37+00:00

I have an IEnumerable<MyObject> collection, with N MyObject elements. MyObject is a class with

  • 0

I have an IEnumerable<MyObject> collection, with N MyObject elements.
MyObject is a class with a Title, a Description and an ID (as string).

I’d like to have my collection with distinct list of MyObject, due to the ID field.
So if 2 MyObject have got the same ID, one should be deleted (don’t care which, I need unique ID).

How can I do it with LINQ?

Tried :

myList = myList.GroupBy(o => o.ID); 

but seems I need a cast?

  • 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-08T01:43:39+00:00Added an answer on June 8, 2026 at 1:43 am

    You can implement a custom IEqualityComparer<MyObject>. Then you can use Enumerable.Distinct to filter out duplicates.

    class DistinctIdComparer : IEqualityComparer<MyObject> {
        public bool Equals(MyObject x, MyObject y) {
            return x.Id == y.Id;
        }
    
        public int GetHashCode(MyObject obj) {
            return obj.Id.GetHashCode();
        }
    }
    

    Now it’s simple:

    IEnumerable<MyObject> distinct = myObjects.Distinct(new DistinctIdComparer());
    

    Or you can use Enumerable.GroupBy what is even simpler:

    distinct = myObjects.GroupBy(o => o.ID)
                        .Select(g => g.First());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this public SomeClass { private List<string> _strings = new
I have an IEnumerable<KeyValuePair<string,string>> , from which I would like, ideally, an anonymous object
I have an interface function that returns an IEnumerable<MyObject> collection. Some of my classes
I have a collection of objects as Dim XXX As IEnumerable(Of MyObject) which has
I have List<MyObject> listObjects where I would like a unique list of MyObject.CategoryName as
I have Ienumerable<string> collection that I want to concatenate into a single string with
I have a IEnumerable<T> collection with Name, FullName and Address. The Address looks like
I have an IEnumerable object ( IEnumerable<Class> ) and I would like to retrieve
Given: I have a class: class MyObject { public string Field1; public string Field2;
I have a List of type MyObject with a definition below: class MyObject {

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.