It seems that i\'m missing something trivial.
Anyway, here it goes:
var order = new[]{1,3,2};
var foos = new[]{new Foo{Id=1}, new Foo{Id=2}, new Fo
You can do this using a nested query, but it is quite inefficient with O(n²)
.
var result = order.Select(o => foos.Single(f => f.Id == o));
If 'order' may contain ids not present in 'foos', you should use SingleOrDefault()
. If foos
might contain duplicate ids, you should use First()
or FirstOrDefault()
.
var result = order
.Select(o => foos.FirstOrDefault(f => f.Id == o))
.Select(f => f != null);
Maybe even a join will work, but I am not sure if it preserves the order.
var result = Enumerable.Join(order, foos, o => o, f => f.Id, (o, f) => f);
As Jon mentioned, the join will only work correctly if the input is well formed in the same way as required by my first suggestion.