2

I need to do a select distinct where I compare 2 values. In the method I do the following:

DistinctErrors = Errors.Select(o => new { o.Type, o.References })
                       .Distinct()
                       .Count();

o.Type is a string, but o.References is a Collection of objects. Each object in o.References has a Name property as string and a Value Property as string, and it's actually o.Type and each o.Reference's Name and Value values I want to compare, so it's actually 3 values to compare.

How can I compare Type with each Reference Name and Value?

Thanks, Peter

5
  • So you want the number of items in Errors that have a distinct Type and a distinct collection of References? Or do you want the number of distinct references in Errors by Type, Name, and Value? An example would help here. Commented Feb 10, 2016 at 17:41
  • Override the Equals and GetHashCode methods in the type you are working. Distinct will see the content of these methods to execute. Commented Feb 10, 2016 at 17:43
  • Alternative to the @FelipeOriani way for comparison is to provide distinct with IEqualityComparer, that is more useful if there are multiple versions of comparison algorithms. In both cases, it has advantage, that it simplifies the code: Errors.Distinct().Count(); or Errors.Distinct(instance IEqualityComparer).Count(); Commented Feb 10, 2016 at 20:53
  • Perhaps the best thing you can do is to provide us with an algorithm, a simple method, that compares two objects in Errors... bool Compare(Error a, Error b) { ... } From there we can manage to help you:). Commented Feb 10, 2016 at 20:59
  • #juharr Errors is an ObservableCollection<T> where each item contains several properties. One of the properties is Type, that is a string. Another one is References, that is an IEnumerable<T>. Each Reference item has 2 properties, string Name and string Value. For each Error item the properties Type, References.Name and References.Value can be the same/appear several times. DistinctErrors is just a number saying how many distinct errors I have in my Errors Collection, i.e.how many unique errors I have. I hope this clarifies. Commented Feb 11, 2016 at 10:53

1 Answer 1

1

Perhaps you want to flatten the references using SelectMany?

DistinctErrors = Errors.SelectMany(o => o.References, (o, r) => new {o.Type, r.Name, r.Value})
                       .Distinct()
                       .Count();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but this doesn't solve my problem since I only get to the Collection References, and not to each item in References, which means I don't find r.Name and r.Value.
Are you sure you copied it properly? I created a sample structure (class with a References collection property) and it worked for me. Can you add the types of Errors and References to your question?
With the risc of portraying myself as a complete idiot...my mistake was that References is/was an IEnumerable<object>, so of course it didn't work. I changed that and now of course it's working. Thanks a lot for the help!! :)
@PeterCentellini OK That explains why it wasn't working - glad you figured it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.