我试图在我的数组中的另一列中找到与最大值对应的值,但是使用当前代码得到错误,说明无法转换类型为WhereSelectArrayIterator
的对象2 [Main.AllClasses.MaxMin,System .Int32]键入' System.IConvertible' .`
我正在为数组分配类型MaxMin
的列表,然后尝试计算第二列中的maxima
及其对应元素。
这是代码:
MaxMin finalMax = new MaxMin();
if (maxima.Count != 0)
{
MaxMin[] maxArray = maxima.ToArray();
finalMax.val = maxArray.Select(x => x.val).Max();
finalMax.cnt = Convert.ToInt32(maxArray.Where(x => x.val.Equals(finalMax.val)).Select(x => x.cnt));
}
这里是完整的代码:
public MaxMin Maxima(float[] values, MaxMin drop)
{
float[] T = new float[5] { 0, 0, 0, 0, 0 };
float[] dT = new float[4] { 0, 0, 0, 0 };
int count = 0;
List<MaxMin> maxima = new List<MaxMin>();
count = values.Length;
try
{
for (int i = 99; i < count - 100; i++)
{
MaxMin max = new MaxMin();
T[0] = values[i - 1];
T[1] = values[i];
T[2] = values[i + 1];
T[3] = values[i + 2];
T[4] = values[i + 3];
dT[0] = T[1] - T[0];
dT[1] = T[2] - T[1];
dT[2] = T[3] - T[2];
dT[3] = T[4] - T[3];
if (i > drop.cnt) //cutoff time above which calculation of minima will be initiated
{
if (dT[1] > 0 && dT[3] < 0)
{
float[] diff = new float[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
diff[0] = (values[i + 1] - values[i - 19]);
diff[1] = (values[i + 1] - values[i + 19]);
diff[2] = (values[i + 1] - values[i - 49]);
diff[3] = (values[i + 1] - values[i + 49]);
diff[4] = (values[i + 1] - values[i - 69]);
diff[5] = (values[i + 1] - values[i + 69]);
diff[6] = (values[i + 1] - values[i - 99]);
diff[7] = (values[i + 1] - values[i + 99]);
if (diff[0] < 20 && diff[1] < 20 && diff[2] < 20 && diff[3] < 20 && diff[2] > 0 && diff[3] > 0 && diff[4] > 0.5 && diff[5] > 0.5 && diff[6] > 2 && diff[7] > 2)
{
max.cnt = i + 1;
max.val = values[i + 1];
maxima.Add(max);
}
}
}
}
MaxMin finalMax = new MaxMin();
if (maxima.Count != 0)
{
MaxMin[] maxArray = maxima.ToArray();
finalMax.val = maxArray.Select(x => x.val).Max();
finalMax.cnt = Convert.ToInt32(maxArray.Where(x => x.val.Equals(finalMax.val)).Select(x => x.cnt));
}
else
{
finalMax.val = values.Max();
finalMax.cnt = Array.IndexOf(values, values.Max());
}
return finalMax;
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:1)
您正在尝试将Select
方法调用的结果转换为IEnumerable<T>
,并将int
转换为First
。那不行。
使用Where
代替Select
/ finalMax.cnt = maxArray.First(x => x.val.Equals(finalMax.val)).cnt;
:
{{1}}