としてGetIndices
の実装を書き換えるしなければならないことを意味、私はあなたがFunc<T,int,bool>
をしたいと思うあなたのスペックから、しかし、あなたをFunc<int,bool>
が必要になります元のインデックスを取得する必要があります。 Funct<T,int,bool>
の条件は、クエリで作成された匿名型ではなく、アイテムとそのインデックスを認識する必要があるため、条件が少し変わります。また、インデックス== 0であるため、先行する項目(index-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)
});
}
}
これは 'someList'が大きい場合は、ここでのパフォーマンス上の問題を導入している可能性があることを小さな警告で、動作するはずは – jeroenh