If I have an IEnumerable<Foo> allFoos and an IEnumerable<Int32> bestFooIndexes, how can I get a new IEnumerable<Foo> bestFoos containing the Foo entries from allFoos at the indexes specified by bestFooIndexes?
If I have an IEnumerable<Foo> allFoos and an IEnumerable<Int32> bestFooIndexes , how can I
Share
Elisha’s answer will certainly work, but it may be very inefficient… it depends on what
allFoosis implemented by. If it’s an implementation ofIList<T>,ElementAtwill be efficient – but if it’s actually the result of (say) a LINQ to Objects query, then the query will be re-run for every index. So it may be more efficient to write:You could to this only when required, of course: