YoungMetroid
YoungMetroid

Reputation: 15

Linq Get the Max() Value of a Group and assign it to that group

This is what i have and works.

foreach (var group in groupCollection)
        {
            int maxValue = group.Max(x => {
                int value = 0;
                if (Int32.TryParse(x[index1].ToString(), out value))
                    return value;
                return value;
            }) ;
            foreach (var row in group)
            {
                row[index2] = maxValue;
            }
        }

But i was wondering if theres a way to simplify this: The following code just assigns what each list already has not the Max value.

groupCollection.ForEach(x => x.Max(y=> {
            int value = 0; 
            if(Int32.TryParse(y[index1].ToString(), out value))
                y[index2] = value;
            return value;
            }));

Keep in mind that groupCollection is a List<List<List<object>>>

Upvotes: 1

Views: 77

Answers (1)

J Stevens
J Stevens

Reputation: 26

Try this:

groupCollection.ForEach(x => {
    int maxValue = x.Max(y =>
        Int32.TryParse(y[index1].ToString(), out var value) ? value : 0);
    x.ForEach(y => y[index2] = maxValue);
    });

Upvotes: 1

Related Questions