0

I am trying to find the value corresponding to maximum value in another column in my array, but get an error with the present code saying Unable to cast object of type WhereSelectArrayIterator2[Main.AllClasses.MaxMin,System.Int32] to type 'System.IConvertible'.`

I am assigning a list of type MaxMin to an array and then trying to calculate the maxima and its corresponding element in the second column. Here's the code:

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));
}

Here's the full code:

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;
        }
  }
1
  • You may need to do something like finalMax.val = maxArray.Select(x => x.val).Max().<some field>; Commented Mar 20, 2014 at 3:58

1 Answer 1

1

You're trying to convert results of Select method call, which returns IEnumerable<T>, to int. That's not gonna work.

Use First instead of Where/Select:

finalMax.cnt = maxArray.First(x => x.val.Equals(finalMax.val)).cnt;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.