Search code examples
c#arrayslistlinqdata-structures

Flattening object of arrays


I have an object "Compact" that consists of an ID and two arrays:

public class Compact
    {
        public int Id { get; set; }
        public int[] Number { get; set; }
        public double[] Value { get; set; }

        public Compact(int id, int[] number,  double[] value)
        {
            Id = id;
            Number = number;
            Value = value;
        }
    }

A list of Compact objects can be initialized like this:

List<Compact> c = new List<Compact>
        {
            new Compact(1, new int[] { 1, 2, 3 }, new double[] { 0.05, 0.02, 0.03 }),
            new Compact(2, new int[] { 1, 2, 3 }, new double[] { 0.07, 0.01, 0.04 }),
            new Compact(3, new int[] { 1, 2, 3 }, new double[] { 0.02, 0.025, 0.05 })
        };

I am looking for an elegant way to flatten this object into the object "Flattened":

public class Flattened
    {
        public int Id { get; set; }
        public int Number { get; set; }
        public double Value { get; set; }

        public Flattened(int id, int number, double value)
        {
            Id = id;
            Number = number;
            Value = value;
        }
    }

The result of the flattening would correspond to this:

List<Flattened> f = new List<Flattened>
        {
            new Flattened(1,1,0.05),
            new Flattened(1,2,0.02),
            new Flattened(1,3,0.03),
            new Flattened(2,1,0.07),
            new Flattened(2,2,0.01),
            new Flattened(2,3,0.04),
            new Flattened(3,1,0.02),
            new Flattened(3,2,0.025),
            new Flattened(3,3,0.05)
        };

I am able to do the flattening using loops, but I am wondering whether there are more suited methods in terms of performance, like a Linq operation or a different collection-type?


Solution

  • You can run simply for loops.

    List<Compact> ls = new List<Compact>
    {
        new Compact(1, new int[] { 1, 2, 3 }, new double[] { 0.05, 0.02, 0.03 }),
        new Compact(2, new int[] { 1, 2, 3 }, new double[] { 0.07, 0.01, 0.04 }),
        new Compact(3, new int[] { 1, 2, 3 }, new double[] { 0.02, 0.025, 0.05 })
    };
    List<Flattened> ls2 = new List<Flattened>();
    foreach (Compact compact in ls)
    {
        for (var index = 0; index < compact.Number.Length; index++)
        {
            ls2.Add(new Flattened(compact.Id, compact.Number[index], compact.Value[index]));
        }
    }
    

    Note I have assumed that both arrays will have same number of items.