【发布时间】:2010-03-18 16:30:47
【问题描述】:
给定这样的数据源:
var c = new Car[]
{
new Car{ Color="Blue", Price=28000},
new Car{ Color="Red", Price=54000},
new Car{ Color="Pink", Price=9999},
// ..
};
如何使用 LINQ 找到满足特定条件的第一辆车的 index?
编辑:
我能想到这样的事情,但看起来很可怕:
int firstItem = someItems.Select((item, index) => new
{
ItemName = item.Color,
Position = index
}).Where(i => i.ItemName == "purple")
.First()
.Position;
用普通的旧循环解决这个问题最好吗?
【问题讨论】:
-
即使这些信息也会有所帮助 - stackoverflow.com/questions/4049773/…
-
其实有一个
index声明:var result = items.Select((item, index) => new { index, item });