0

I have a list of objects.

public class UserProperties
{
    public int Id { get;set; }
    public string Name { get;set; }
    public string ColorCode { get; set; }
}

List<UserProperties> userList = new List<UserProperties>();

I need to take a distinct list from this. Here, Id field is a unique field. Is there any possibility to take distinct list?

If it is list of strings means, then use the distinct. It will work. But in my case, it is a list of objects. We can't use the distinct directly.

  • 5
    _"I look into several sites regarding this. But that solution is not applicable to me."_ - What solutions did you find? Why weren't they applicable? – ProgrammingLlama Sep 23 '19 at 05:35
  • https://stackoverflow.com/questions/24901975/distinct-list-of-object-in-c-sharp, https://www.c-sharpcorner.com/UploadFile/8c19e8/different-ways-to-get-distinct-values-from-a-listt-using/, https://stackoverflow.com/questions/13438673/how-to-get-the-distinct-data-from-a-list ..........I looked into this sites. – Nivitha Gopalakrishnan Sep 23 '19 at 05:39
  • https://stackoverflow.com/questions/489258/linqs-distinct-on-a-particular-property – Sean Missingham Sep 23 '19 at 05:42
  • 1
    And why aren't they applicable? Especially [this answer](https://stackoverflow.com/a/24902066/3181933), which seems to be the exact solution you need? _"We can't use the distinct directly."_ - you absolutely can if you prepare your class for it, as described in the question you linked to. – ProgrammingLlama Sep 23 '19 at 05:45
  • Absolutly not a duplicate! You can implement a unique list of UserProperties like this while exposing every methods of a List you need to use. public class UserPropertiesList : IEnumerable { private readonly List Items = new List(); ... With at least enumerators, Count, this, Add that check if Items contains already an object with same ID "if ( Items.Count(up => up.Id == item.Id) != 0 ) throw new Exception" and Remove. –  Sep 23 '19 at 06:01
  • @Olivier Why isn't it a duplicate? And how does your suggestion solve the problem OP is describing better than using `.Distinct` does? The solution I specifically pointed to would also allow OP to build a `HashSet`, which would use the object's GetHashCode / Equals methods, or a custom `IEqualityComparer`. – ProgrammingLlama Sep 23 '19 at 06:03
  • He not want to take distincts, he want to disallow adding an item if the list has already an item with same Id ! HashSet does not solve problem. –  Sep 23 '19 at 06:05
  • @Olivier No, OP hasn't stated that requirement anywhere in the question or the comments. And no, HashSet _is not_ based on the unique reference. – ProgrammingLlama Sep 23 '19 at 06:05
  • Yes, he specified what he needs : "Here, Id field is a unique field". When it is written "I need to take a distinct list from this", it means having a list of items with IDs without duplicates on IDs. –  Sep 23 '19 at 06:06
  • @Olivier [A demonstration of how Distinct and HashSet work](https://rextester.com/CRCXP8779). – ProgrammingLlama Sep 23 '19 at 06:11
  • @Olivier Yes, exactly. He wants a distinct list. a `HashSet` or LINQ's `.Distinct()` would be the perfect tool for the job. Therefore this question is a duplicate. – ProgrammingLlama Sep 23 '19 at 06:12
  • @Olivier In the linked sample, I also included using `.Distinct()` which operated on a `List`. The default equality comparer for any object uses the type's `GetHashCode` and `Equals` methods. If they aren't implemented then it uses the base `object`. That's why you can't compare a custom type where you haven't overloaded these (or specified a comparer for the object). – ProgrammingLlama Sep 23 '19 at 06:14
  • Never used HashSet but ok, it solves the problem too. But remind List and HashSet are a little different... You have not indexer. And Distinct allows to take distincts on a list with duplicates so it is surely not a solution here. So it is not a duplicate of Distinct solution. –  Sep 23 '19 at 06:14
  • @Olivier _"Distinct allows to take distincts on a list with duplicates so it is surely not a solution here."_ - OP hasn't mentioned mutating the list in their question. OP said _"I need to take a distinct list from this"_ - i.e. "I have a list, which I need to get a list of distinct items from.", exactly what `.Distinct` is for. – ProgrammingLlama Sep 23 '19 at 06:16
  • You may ask him clarification about that because unique ID equals to unique list, not taking distinct of a not unique list... So HashSet and Distinct are not a solution, it seems. Especially if he want an indexer. –  Sep 23 '19 at 06:17
  • @Olivier What would be the point in using `Distinct` on a unique list? You'd get the same list back. If OP wants a better solution, then OP is welcome to edit their question to be specific about what they're asking. At the moment I feel like you're reading more into this question than there is. – ProgrammingLlama Sep 23 '19 at 06:18
  • 1
    If the question is to have a list with unique Ids and an indexer, the solution has been given in comment as implementing a wrapper to a List<>. –  Sep 23 '19 at 06:20
  • @Olivier At least we're certain that this question should be closed. :-) If we can't agree on what OP wants to do then it's unclear. – ProgrammingLlama Sep 23 '19 at 06:21

0 Answers0