指定的转换无效,从交错数组中获取所有值

Specified cast is not valid, getting all values from jagged array

指定的转换无效,请告诉我如何从数组中存储的所有值而不是特定索引中获取 max/min 值。

int max = jArray.Cast<int>().Max(); 
                System.Console.Write("\n\n Max marks:" + max );

交错数组的声明:

 string TotalStudents;

        System.Console.Write("Enter the Total No. Of Students:");
        TotalStudents = Console.ReadLine();

        int value;
        bool result = int.TryParse(TotalStudents, out value);

        JaggedArray jag = new JaggedArray(value);

        int[][] jArray = new int[jag.noOfStudents][];



        for (int i = 0; i < jag.noOfStudents; i++)
        {


            System.Console.Write("Enter the Total No. Of Subjects of Student:" + i + ":\t");
            string TotalSubjects = Console.ReadLine();

            int Subjectvalue;
            bool Sresult = int.TryParse(TotalSubjects, out Subjectvalue);
            jArray[i] = new int[Subjectvalue];

            for (int a = 0; a < Subjectvalue; a++)
            {
                System.Console.Write("\nEnter the marks obtained of subject:" + a + " of student " + i + ":\t");
                string TotalMarks = Console.ReadLine();

                int Marksvalue;
                bool Mresult = int.TryParse(TotalMarks, out Marksvalue);
                jArray[i][a] = Marksvalue;

            }

JArray 是一个参差不齐的数组(数组的数组),这就是为什么特定转换为 int 是无效的。

我建议使用 SelectMany 展平结构并查找 Max.

int max = jArray.SelectMany(x=>x.ToArray()).Max();