Suppose I have one collection, call it ids
it is of type IEnumerable<string>
, I have a second collection call it objects
it's of type MyObject[]
. MyObject
has a string property called id
. I would like a LINQ statement that returns all off the objects in the objects
collection who's id
matches any value in the ids
collection. ids
will be a strict subset of objects.Select(x => x.id)
. Meaning, for every string in ids
I know there will be exactly one corresponding MyObject
in objects
. Can someone post a pure LINQ solution? I've tried a couple things with no luck. I can come up with an iterative solution easily enough so unless it's impossible to do with only LINQ please don't post any.
"Just" LINQ:
var r = obj.Where(o => ids.Any(id => id == o.id));
But better, for larger n, with a set:
var hs = new HashSet(ids);
var r = obj.Where(o => hs.Contains(o.id));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With