❔ Get list of indices from a List
I have a result set called "result". I want to use Linq to get a list of distinct items by name and their indices in the source resultset. How can I accomplish that?
15 Replies
how would this work? first of all, a set already only contains distinct items. if you mean that you have a list, and you then got the items distinct by their name, which distinct item's index would you want? the first? second? nth?
Something like
collection.DistinctBy(item => item.Name).Select(item, index => new { Name = item.Name, Index = index})
but that's incorrect
that would return the index in the new distinct collection
like what would the result look like here?
I think they mean collection
?
Just because of the mentioning of Distinct
what about it
You said it already a set would have distinct items
Hence I believe they just meant a collection of things
sure
doesn't really matter
your version would be incorrect in any case
their indices in the source resultsetthis part just doesn't really make sense
Ah good point
Yeah it doesnt 😅
it's fine that they want the index in the source collection, but if you expect the item to exist more than once, then you need to have a concept of which one you want
Exactly
Hi, thank you for the guidence. I was able to work it out following what was said and some search:
i mean again like, it's not really clear if this is the desired solution
either way you should prefer using a valuetuple
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.