-4

I have a list that contains 1200 items,

I need to remove all of the duplicate items in the list that have a certain predicate, my items have the following status on them:

"not flat", "not covered", "accepted", X, Y, Z

i want to remove all duplicate item from the list that have the same X,Y,Z and have the same Status string

how can I achieve this?

I tried to do this this way:

public class MyEqualityComparer : IEqualityComparer<TcReportPoint3D>
{
    public bool Equals(TcReportPoint3D a, TcReportPoint3D b)
    {
        return a.X == b.X && a.Y == b.Y && a.Z == b.Z && a.Status != b.Status;
    } 

    public int GetHashCode(TcReportPoint3D other)
    {
        return other.X.GetHashCode() * 19 + other.Y.GetHashCode();
    }
}

then:

  //get all distinct values with different status
    var points = reportpoints.Distinct(new MyEqualityComparer()).ToList();

    //remove distinct values from the real least, hoping to remove duplicates.
    foreach (var point in points)
    {
        if (point.Status == TePointStatus.NotCovered
            || point.Status == TePointStatus.OutOfSigma
            || point.Status == TePointStatus.NotFlat)
            reportpoints.Remove(point);
    }

The reason is that i have a list of items that was combined from a logic of two conditions, where the same value will have a different status then the other one.

I want to somehow be able to get the differences out of the list, then remove the duplicates that match the status.

Dean
  • 499
  • 6
  • 13
  • 34
  • What have you tried so far? What problems are you having? SO is not a write-my-code-for-me site. – Craig W. Jul 30 '15 at 23:07
  • @Dean when you do List.Remove, you only the first occurrence of the element. This means if you have a List A,A,A and call list.Remove("A"), the remaining list will still contain two "A"s, rights? Ref https://msdn.microsoft.com/en-us/library/cd666k3e(v=vs.110).aspx. After you have a list of distinct points, do you need to revisit the reportPoints list? – KnightFox Jul 31 '15 at 06:13

2 Answers2

1
var result = ObjectsWithDuplicates.Select(o => o.StringName).Distinct().ToList();

Or

var result = ObjectsWithDuplicates.GroupBy(o => o.StringName).Select(oo => oo.First()).ToList();
CharithJ
  • 46,289
  • 20
  • 116
  • 131
0

Here is a simple solution:

List<string> values = new List<string>();
            values.Add("not flat");
            values.Add("not covered");
            values.Add("accepted");
            values.Add("accepted");
            values.Add("not flat");
            values.Add("not flat");
            values.Add("not covered");
            values.Add("not flat");

 List<string> distinctValues = values.Distinct().ToList();

Debug:

enter image description here

JGV
  • 5,037
  • 9
  • 50
  • 94