I am using the extension method in below link to pivot my data: https://techbrij.com/pivot-c-array-datatable-convert-column-to-row-linq
I am including the code from the link just in case somebody finds this question in the future and the link is dead:
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(
rowGroup,
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();
table.Rows.Add(dataRow);
}
return table;
}
Referring to the example in the link, you get the pivoted data like;
var pivotTable = data.ToPivotTable(
item => item.Year,
item => item.Product,
items => items.Any() ? items.Sum(x=>x.Sales) : 0);
My question is, how can I include more rows into this query to return for example, ProductCode as well.. item => new {item.Product, item.ProductCode}
does not work..
============== EDIT / 23 OCT 2018 ==============
Assuming my data is this;
With the help of the above mentioned code, I can manage to do this;
What I want to achieve is this (extra col: STOCKID or any other cols as well);
Anonymous types cannot be passed as generic parameters. Try defining your pivot key as a struct:
public struct PivotKey
{
public string Product;
public int ProductCode; // assuming your product codes are integers
}
This way you can take advantage of struct's default Equals
and GetHashCode
methods implementation in terms of the equality and hash code of all the fields.
Then, define the rowSelector
as below:
item => new PivotKey { Product = item.Product, ProductCode = item.ProductCode}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With