在子列表 C# 中查找具有最小值的索引
Find index which has minimum value in sublist C#
我想查找子列表中该索引具有最小值的索引。
我有一个像这样的 class 名称子问题:
public class Subproblem
{
public double[,] x { get; set; }
public double ReducedCost { get; set; }
}
然后我想找到具有最小化成本值的子问题的索引。
var sub = new List<Subproblem>();
Subproblem s = new Subproblem();
s.x = new double[DC1, DC1];
for (int m = 0; m < M; ++m)
{
s.ReducedCost = model.ObjVal;
for (int i = 0; i < DC1; ++i)
{
for (int j = 0; j < DC1; ++j)
{
s.x[i, j] = x[i, j].X;
}
}
sub.Add(s);
}
double minRC = sub.Min(a => a.ReducedCost);
int minRCIndex = sub.IndexOf(minRC);
最后一行还是报错,不知道怎么解决
您收到此错误是因为 minRC
具有类型 double
但在您的情况下 IndexOf()
需要参数类型 Subproblem
.
你应该使用 FindIndex()
:
int minRCIndex = sub.FindIndex((i) => i.ReducedCost == minRC);
此外,您可以更改这两行:
double minRC = sub.Min(a => a.ReducedCost);
int minRCIndex = sub.IndexOf(minRC);
只有一个:
int minRCIndex = sub.FindIndex(i => i.ReducedCost == sub.Min(a => a.ReducedCost));
我想查找子列表中该索引具有最小值的索引。 我有一个像这样的 class 名称子问题:
public class Subproblem
{
public double[,] x { get; set; }
public double ReducedCost { get; set; }
}
然后我想找到具有最小化成本值的子问题的索引。
var sub = new List<Subproblem>();
Subproblem s = new Subproblem();
s.x = new double[DC1, DC1];
for (int m = 0; m < M; ++m)
{
s.ReducedCost = model.ObjVal;
for (int i = 0; i < DC1; ++i)
{
for (int j = 0; j < DC1; ++j)
{
s.x[i, j] = x[i, j].X;
}
}
sub.Add(s);
}
double minRC = sub.Min(a => a.ReducedCost);
int minRCIndex = sub.IndexOf(minRC);
最后一行还是报错,不知道怎么解决
您收到此错误是因为 minRC
具有类型 double
但在您的情况下 IndexOf()
需要参数类型 Subproblem
.
你应该使用 FindIndex()
:
int minRCIndex = sub.FindIndex((i) => i.ReducedCost == minRC);
此外,您可以更改这两行:
double minRC = sub.Min(a => a.ReducedCost);
int minRCIndex = sub.IndexOf(minRC);
只有一个:
int minRCIndex = sub.FindIndex(i => i.ReducedCost == sub.Min(a => a.ReducedCost));