score:0

Accepted answer
public static dynamic[] topivotarray<t, tcolumn, trow, tdata>(
    this ienumerable<t> source,
    func<t, tcolumn> columnselector,
    expression<func<t, trow>> rowselector,
    func<ienumerable<t>, tdata> dataselector)
{

       var arr = new list<object>();
       var cols = new list<string>();
       string rowname = ((memberexpression)rowselector.body).member.name;
       var columns = source.select(columnselector).distinct();       

       cols =(new []{ rowname}).concat(columns.select(x=>x.tostring())).tolist();


       var rows = source.groupby(rowselector.compile())
                        .select(rowgroup => new
                        {
                            key = rowgroup.key,
                            values = columns.groupjoin(
                                rowgroup,
                                c => c,
                                r => columnselector(r),
                                (c, columngroup) => dataselector(columngroup))
                        }).toarray();


       foreach (var row in rows)
       {
           var items = row.values.cast<object>().tolist();
           items.insert(0, row.key);
           var obj = getanonymousobject(cols, items);
           arr.add(obj);               
       }
       return arr.toarray();
}

Related Query

More Query from same tag