score:2

Accepted answer

There can be multiple Values for each column, and there can be multiple columns for each Title.

That means that you have to repeat the first step and GroupBy again by columns. You also should use your types that you already implemented for this. Don't use anonymous types:

IEnumerable<MyClass> temp = records
    .GroupBy(x => x.Title)
    .Select(y => new MyClass
    {
        Title = y.Key,
        Columns = y.GroupBy(x => x.Column)
        .Select( c => new Column
        {
            Column_ = c.Key,
            Values = c.Select(v => new Value
            {
                Value_ = v.Value
            }).ToList()
        }).ToList()
    });

    temp.Dump();

Result (from the LINQPad Dump):

enter image description here

PS. I changed the names of the properties, because the compiler does not allow them to be named exactly as the class is.

class Column
{
    public string Column_ { get; set; }
    public List<Value> Values { get; set; }
}
class Value
{
    public string Value_ { get; set; }
}

Related Articles