Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取列表中最大元素的索引<;T>;_C#_List_Max_Indexof - Fatal编程技术网

C# 如何获取列表中最大元素的索引<;T>;

C# 如何获取列表中最大元素的索引<;T>;,c#,list,max,indexof,C#,List,Max,Indexof,我有一个列表,类名为Product,我想知道元素的索引,它的最大值是多少 类产品 { public int ProductNumber{get;set;} public int ProductSize{get;set;} } List productList=新列表(); int Index=productList.Indexof(productList.Max(a=>a.ProductSize)); 我试过了,但没有得到答案!并得到一个错误: “无法转换为产品” 方法Max将为您提供最大Pr

我有一个
列表
,类名为
Product
,我想知道元素的索引,它的最大值是多少

类产品
{
public int ProductNumber{get;set;}
public int ProductSize{get;set;}
}
List productList=新列表();
int Index=productList.Indexof(productList.Max(a=>a.ProductSize));
我试过了,但没有得到答案!并得到一个错误:

“无法转换为产品”


方法
Max
将为您提供最大ProductSize,而不是Product的实例。这就是您收到此错误的原因

您可以使用降序操作来执行此操作:

var item = productList.OrderByDescending(i => i.ProductSize).First();
int index = productList.IndexOf(item);

方法
Max
将为您提供最大ProductSize,而不是Product的实例。这就是您收到此错误的原因

您可以使用降序操作来执行此操作:

var item = productList.OrderByDescending(i => i.ProductSize).First();
int index = productList.IndexOf(item);
这需要一种

var maxObject = productList.OrderByDescending(item => item.ProductSize).First();
var index = productList.IndexOf(maxObject);
还有其他更简单的方法可以做到这一点。例如:中有一个扩展方法可以实现这一点

请参见问题

这需要排序

var maxObject = productList.OrderByDescending(item => item.ProductSize).First();
var index = productList.IndexOf(maxObject);
还有其他更简单的方法可以做到这一点。例如:中有一个扩展方法可以实现这一点


请参阅问题

productList.Max(a=>a.ProductSize)将返回最大ProductSize值,而不是Product对象。该条件应位于WHERE条件中。

productList.Max(a=>a.ProductSize)将返回最大ProductSize值,而不是Product对象。该条件应为WHERE条件。

您可以首先映射每个项目,使每个产品与其索引相关联,然后按降序排序并获得第一个项目:

int Index = productList
    .Select((x, index) => new { Index = index, Product = x })
    .OrderByDescending(x => x.Product.ProductSize).First().Index;

您不需要再次调用
IndexOf

您可以首先映射每个项目,使每个产品与其索引相关联,然后按降序排序并获得第一个项目:

int Index = productList
    .Select((x, index) => new { Index = index, Product = x })
    .OrderByDescending(x => x.Product.ProductSize).First().Index;

您不需要再次调用
IndexOf

您正在寻找的是
ArgMax
,它不是在Linq中实现的,但是可以通过
Aggregate
轻松模拟:

  int Index = productList
    .Select((item, index) => new { item, index })
    .Aggregate((s, v) => v.item.ProductSize > s.item.ProductSize ? v : s)
    .index;

您正在寻找的是
ArgMax
,它没有在Linq中实现,但可以通过
Aggregate
轻松模拟:

  int Index = productList
    .Select((item, index) => new { item, index })
    .Aggregate((s, v) => v.item.ProductSize > s.item.ProductSize ? v : s)
    .index;

下面是一个具有
可枚举范围的解决方案

int index = Enumerable.Range(0, productList.Count)
                      .FirstOrDefault(i => productList[i].ProductSize == productList.Max(x => x.ProductSize));

这是一个具有可枚举范围的
解决方案

int index = Enumerable.Range(0, productList.Count)
                      .FirstOrDefault(i => productList[i].ProductSize == productList.Max(x => x.ProductSize));

假设列表不是空的:

productList.Indexof(productList.OrderByDescending(a => a.ProductSize).First());

假设列表不是空的:

productList.Indexof(productList.OrderByDescending(a => a.ProductSize).First());

你的意思是:
productList.OrderByDescending(i=>i.ProductSize).First()
?你的意思是:
productList.OrderByDescending(i=>i.ProductSize).First()
?如果性能很重要,那么做一些类似于
var indexOfMax=productList.Select((项,索引)=>new{item,index})。聚合((a,b)=>a.item.ProductSize>b.item.ProductSize?a:b).索引如果性能很重要,请执行类似于
var indexOfMax=productList的操作。选择((项,索引)=>new{item,index})。聚合((a,b)=>a.item.ProductSize>b.item.ProductSize?a:b)。索引