0

I need to remove the repeated values from the list lista and put them in another list or in the same.

Distinct() doesn't work. listasemrepetidos returns the same as lista.

List<XYZr> lista = new List<XYZr>();
List<XYZr> listasemrepetidos = new List<XYZr>();   
lista.Add(new XYZr(Convert.ToDouble(data[1].Replace('.', ',')), Convert.ToDouble(data[2].Replace('.', ',')), Convert.ToDouble(data[3].Replace('.', ',')), CLR));

 listasemrepetidos = lista.Distinct().ToList<XYZr>();
David Tansey
  • 5,813
  • 4
  • 35
  • 51
Tiago
  • 1
  • 6
    Have you read [the documentation](https://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx) for Distinct? It clearly explains what you need to do to handle custom types. Always go to the documentation first if you have a question about how something built into the framework works. – mason Jun 28 '17 at 15:38
  • 3
    Every object you add to the list are distinct objects, so there are no duplicates. – hatchet - done with SOverflow Jun 28 '17 at 15:40
  • 1
    Typically this is solved by **grouping** on a specific property, and **selecting** the **first** item in each group for your output (hint hit `GroupBy()` and `Select(x => x.First())` are your friends here). – maccettura Jun 28 '17 at 15:45
  • firstly You will need nice shiny comparer to compare your object. use Distinct with that comparer. listasemrepetidos = lista.Distinct(new XYXrComparer()).ToList(); – dipak Jun 28 '17 at 16:38
  • Tiago: the code you posted has only a single element in `lista`. It's difficult to understand what you expected to happen, other than getting another list with that same element in it. `Distinct()` removes duplicates, but none could exist in a sequence having only one element. So the code in your question fails to meet the [mcve] standard. On the assumption that your real scenario has actual duplicates, the marked duplicate questions address the most common mistake people make, and thus likely applies in your case. If not, post a new question in which you provide better information. – Peter Duniho Jun 28 '17 at 16:42

0 Answers0