給定這樣的數(shù)據(jù)源:var c = new Car[]{ new Car{ Color="Blue", Price=28000}, new Car{ Color="Red", Price=54000}, new Car{ Color="Pink", Price=9999}, // ..};如何用LINQ 找到滿足一定條件的第一輛車的索引?編輯:我可以想到這樣的東西,但它看起來很糟糕:int firstItem = someItems.Select((item, index) => new { ItemName = item.Color, Position = index }).Where(i => i.ItemName == "purple") .First() .Position;用一個普通的循環(huán)解決這個問題會是最好的嗎?
3 回答

搖曳的薔薇
TA貢獻1793條經(jīng)驗 獲得超6個贊
簡單地說:
int index = List.FindIndex(your condition);
例如
int index = cars.FindIndex(c => c.ID == 150);
- 3 回答
- 0 關(guān)注
- 1150 瀏覽
添加回答
舉報
0/150
提交
取消