I have two collections: one is Items And Another is ActiveItems
The only intersection between these two collection is Name
I want a list with Linq from Items where the Items names are in the ActiveItems with that name
I wrote this code is there a better idea:
Items.Where(i => ActiveItems.Count(v=> v.Name==i.Name) > 0)
I would probably create a set of the names from
ActiveItemsand then use that:Another option is to use a join, e.g. with a query expression:
Note that this will give duplicate
itemvalues if there are multipleActiveItemvalues with the same name. Another alternative join, which doesn’t have this problem but is a bit clumsier:Note that all of these will avoid the O(N * M) check for names – they’ll all use hash tables behind the scenes, to give an O(N + M) complexity.