Como jeroenh se dio cuenta, se necesidad de capturar el índice original. La condición Funct<T,int,bool>
que pase solo necesita conocer el elemento y su índice, no el tipo anónimo creado en la consulta, por lo que la condición aprobada cambia un poco. También debe manejar la situación donde el índice == 0 y, por lo tanto, no hay elementos precedentes (índice - 1).
class Program {
static void Main(string[] args) {
var items = Item.GetItems();
// mind the case where index == 0 so you don't grab an item out of bounds
var ind = GetIndices(items,
(p, index) => (h.index == 0) ? false : p.Height < items[ index - 1 ].Height);
}
static List<int> GetIndices<T>(List<T> list, Func<T, int, bool> condition) {
var res = list
.Select((item, index) => new { item, index }) // capture original index
.Where(h => condition(h.item, h.index))
.Select(h => h.index); // reduce to the index again
return res.ToList();
}
}
class Item {
public int Height {
get;
set;
}
public Item(int h) {
Height = h;
}
static public List<Item> GetItems() {
return new List<Item>(new[]{
new Item(1),
new Item(4),
new Item(2),
new Item(5)
});
}
}
esto debería funcionar, con la pequeña salvedad de que si 'someList' es grande que podría haber introducido un problema de rendimiento aquí (' IndexOf' es un O (N) la operación) – jeroenh