Transform Rows to Columns - Pivot in c#

Good day,

I pull the following table from sql with a fixed number of columns (rp1 - rp40) and a column named segment name.

I want to transform it from this format to the below table in c#. Segment name can be anything and will change often, and there could be 20 segment name rows (there for it would transforum to having 20 columns) (i think this is easier than doing in sql and then figuring out how to pull dynamic columns from sql)

sample sql table

Segment rp1 rp2 rp3
USA 1 2 4
Canada 2 3 5

end result in this example

USA Canada
1 2
2 3
4 5

I have tried this, but I am unsure what the trow selection input should be since i need all of them

    public static DataTable ToPivotTable<T, TColumn, TRow, TData>(this IEnumerable<T> source, Func<T, TColumn> columnSelector, Expression<Func<T, TRow>> rowSelector, Func<IEnumerable<T>, TData> dataSelector)
        DataTable table = new DataTable();
        var rowName = ((MemberExpression)rowSelector.Body).Member.Name;
        table.Columns.Add(new DataColumn(rowName));
        var columns = source.Select(columnSelector).Distinct();

        foreach (var column in columns)
            table.Columns.Add(new DataColumn(column.ToString()));

        var rows = source.GroupBy(rowSelector.Compile())
                         .Select(rowGroup => new {
                             Key = rowGroup.Key,
                             Values = columns.GroupJoin(
                                 c => c,
                                 r => columnSelector(r),
                                 (c, columnGroup) => dataSelector(columnGroup))

        foreach (var row in rows)
            var dataRow = table.NewRow();
            var items = row.Values.Cast<object>().ToList();
            items.Insert(0, row.Key);
            dataRow.ItemArray = items.ToArray();

        return table;