locked
Help with LINQ conversion of for loop RRS feed

  • Question

  • User404523993 posted

    For the purpose of finding the highest number in a my chart data (stored in a hashtable), I wrote a quick routine to get me back the largest number as follows:

            public void GetBarMaximum(Hashtable chartData, ref double axisMax)
            {
                // get array length from first row of data...
                int arrayLength = ((double[])chartData[1]).Length;
    
                // declare array to hold sum of each array index...
                double[] columnSums = new double[arrayLength];
    
                // loop through each row of the hashtable...
                for (int i = 1; i <= chartData.Count; i++)
                {
                    // get the row of data...
                    double[] dataColumns = (double[])chartData[i];
    
                    // loop through the items in the row...
                    for (int j = 0; j < columnSums.Length; j++)
                    {
                        // if value not equal to NaN...
                        if (!dataColumns[j].Equals(double.NaN))
                        {
                            // get the current array index value...
                            double currentValue = columnSums[j];
    
                            // add the new value to it...
                            currentValue += dataColumns[j];
    
                            // add value to array...
                            columnSums.SetValue(currentValue, j);
                        }
                    }
                }
    
                // define and initalise variable...
                double totalValue = 0;
    
                // loop through resulting array...
                for (int i = 0; i < columnSums.Length; i++)
                {
                    // and get the maximum value...
                    totalValue = Math.Max(totalValue, columnSums[i]);
                }
    
                // set output variable value...
                axisMax = totalValue;
            }


     

    Now, I am using the latest version of ReSharper, and it suggested that I could recode my final for loop using LINQ, so I let it perform the conversion. I then noticed that the result was coming out very different.

    The code changed to:

    // get the maximum value using LINQ...
    totalValue = columnSums.Aggregate<double, double>(0, (current, t) => Math.Max((sbyte)current, (sbyte)t));

    An example hashtable is as follows:

    20000, double.NaN

    50000, double.NaN

    1500, double.NaN

    double.NaN, 1500000

    Therefore, my code returns the value 1500000. The LINQ expression returns 96!

    Can anyone shed some light on what the correct LINQ expression should be for this?

    Thanks

    Martin


     


     

    Wednesday, January 26, 2011 6:48 AM

All replies

  • User1224194097 posted

    You can retrieve values from HashTable and use Max extension method to get the max value. check this example.

    Hashtable Hashtable1 = new Hashtable();
    Hashtable1.Add("number1", 123);
    Hashtable1.Add("number2", 1234);
    int number = 0;
    int maxNumber = Hashtable1.Values.Cast<object>()
                                .Where(a => Int32.TryParse(a.ToString(), out number))
                                .Max(b => Convert.ToInt32(b));

    Wednesday, January 26, 2011 8:57 AM
  • User1009619486 posted

    u can also try for this 

    you can replace whole method for this line

    double maxValue = chartData.Values.Cast<object>().Max(obj => (double)obj);

    Wednesday, January 26, 2011 10:20 AM
  • User404523993 posted

    u can also try for this 

    you can replace whole method for this line

    double maxValue = chartData.Values.Cast<object>().Max(obj => (double)obj);

     

    I guessed there would be a better way to do what I was doing. Will give it a try, many thanks.

    Martin

    Wednesday, January 26, 2011 10:31 AM
  • User404523993 posted

    u can also try for this 

    you can replace whole method for this line

    double maxValue = chartData.Values.Cast<object>().Max(obj => (double)obj);

     

    Hi

    This gives me an invalid cast exception - am I missing something?

    Thanks

    Martin

    Thursday, January 27, 2011 4:43 AM