J
J
justmakewow2019-04-12 18:33:05
C++ / C#
justmakewow, 2019-04-12 18:33:05

How to convert to linq?

Tell me, is it possible to somehow concisely convert the GetIntValue method into a linq command?

public class ValueClass
    {
        public int Value;
    }

public int[,] GetIntValue(ValueClass[,] values)
        {
            // можно весь метод заменить на 1 linq команду?
            int[,] valuesInt = new int[2, 2];
            valuesInt[0, 0] = values[0, 0].Value;
            valuesInt[0, 1] = values[0, 1].Value;
            valuesInt[1, 0] = values[1, 0].Value;
            valuesInt[0, 1] = values[0, 1].Value;

            return valuesInt;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
basrach, 2019-04-12
@justmakewow

public int[,] GetIntValue(ValueClass[,] values)
{
  return values.Cast<ValueClass>()
    .Select((x, i) => new { x?.Value, i })
    .Aggregate(
      new int[values.GetLength(0), values.GetLength(1)],
      (accum, item) =>
      {
        accum[item.i / values.GetLength(1), item.i % values.GetLength(1)] = item.Value.GetValueOrDefault();
        return accum;
      });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question