Linq 获取组的 Max() 值并将其分配给该组

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

这就是我的工作。

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

但我想知道是否有办法简化这个: 下面的代码只是分配每个列表已经有的东西 不是最大值。

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

请记住,groupCollection 是一个 List<List<List<object>>>

试试这个:

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