I have a list of "Entry" objects (that each have an ID), and want to create a 2D array based by splitting them up by ID:
class Entry
{
public int ID { get; set; }
public int value { get; set; }
public Entry(int id, int value)
{
this.ID = id;
this.value = value;
}
}
Population:
List<Entry> entries = new List<Entry>();
entries.Add(new Entry(0, 20));
entries.Add(new Entry(1, 20));
entries.Add(new Entry(1, 25));
entries.Add(new Entry(2, 21));
entries.Add(new Entry(2, 23));
I need to build a Multidimensional list of entries split up by ID. The following code builds the array:
List<Entry> entriesZero = new List<Entry>();
entriesZero.Add(new Entry(0, 20));
List<Entry> entriesOne = new List<Entry>();
entriesOne.Add(new Entry(1, 20));
entriesOne.Add(new Entry(1, 25));
List<Entry> entriesTwo = new List<Entry>();
entriesTwo.Add(new Entry(2, 21));
entriesTwo.Add(new Entry(2, 23));
List<List<Entry>> entriesByID = new List<List<Entry>>();
entriesByID.Add(entriesZero);
entriesByID.Add(entriesOne);
entriesByID.Add(entriesTwo);
What would be the best way to accomplish something like this? I could do it with multiple foreach loops but I'm wondering if there's a better way in LINQ.