Peter Centellini
Peter Centellini

Reputation: 1575

How to select distinct with linq when comparing values

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

Upvotes: 2

Views: 946

Answers (1)

D Stanley
D Stanley

Reputation: 152566

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();

Upvotes: 1

Related Questions