简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM