【发布时间】:2018-05-11 06:37:40
【问题描述】:
我有一个List,类名Product,我想知道最大值元素的索引?
class Product
{
public int ProductNumber { get; set; }
public int ProductSize { get; set; }
}
List<Product> productList = new List<Product>();
int Index = productList.Indexof(productList.Max(a => a.ProductSize));
我试过这个,但没有得到答案!并得到一个错误:
“无法投射为产品”
【问题讨论】:
-
如果性能很重要,请执行
var indexOfMax = productList.Select((item, index) => new { item, index }).Aggregate((a, b) => a.item.ProductSize > b.item.ProductSize ? a : b).index;