Click here to Skip to main content
15,881,281 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 3:51
Mou_kol15-Sep-21 3:51 
GeneralRe: C# Confusion about LINQ Grouping Pin
Richard Deeming15-Sep-21 4:15
mveRichard Deeming15-Sep-21 4:15 
AnswerRe: C# Confusion about LINQ Grouping Pin
BillWoodruff15-Sep-21 19:35
professionalBillWoodruff15-Sep-21 19:35 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 20:22
Mou_kol15-Sep-21 20:22 
GeneralRe: C# Confusion about LINQ Grouping Pin
Dave Kreskowiak16-Sep-21 2:25
mveDave Kreskowiak16-Sep-21 2:25 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol16-Sep-21 6:30
Mou_kol16-Sep-21 6:30 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 20:39
Mou_kol15-Sep-21 20:39 
QuestionC# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 6:00
Mou_kol14-Sep-21 6:00 
I like to know how to convert List<t> to Pivot datatable.

this is my sample data populated in List.
C#
List<Data> _data = new List<Data> 
    { 
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "ZB",
            BrokerName = "B Securities",
            Period = "2012 FYA",
            PeriodValue = ""
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "2013 FYA",
            PeriodValue = ""
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "1Q 2014A",
            PeriodValue = "204.45"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "2Q 2014A",
            PeriodValue = "205.00"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "2012 FYA",
            PeriodValue = "101.33"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "2013 FYA",
            PeriodValue = ""
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "1Q 2014A",
            PeriodValue = "204.45"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Net Revenue",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "2Q 2014A",
            PeriodValue = "201.00"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "2012 FYA",
            PeriodValue = ""
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "2013 FYA",
            PeriodValue = ""
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "1Q 2014A",
            PeriodValue = "204.45"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "ZB",
            BrokerName = "B. Riley Securities",
            Period = "2Q 2014A",
            PeriodValue = "201.00"
        },
    
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "2012 FYA",
            PeriodValue = "101.33"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "2013 FYA",
            PeriodValue = "222.30"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "1Q 2014A",
            PeriodValue = "784.45"
        },
        new Data
        {
            Section = "Consensus Model",
            Lineitem = "Cost of Goods Sold",
            BrokerCode = "TU",
            BrokerName = "Cantor Fitzgerald & Co",
            Period = "2Q 2014A",
            PeriodValue = "555.00"
        },
    };


Data grouping should be like Section, Lineitem, BrokerCode & Period
sample periods are like 2012 FYA or 2013 FYA...2Q 2014A

Desired output would be like below example
+-----------------+--------------------+-------------+-------------------+----------+----------+-----------+----------+
    |     Section     |      LineItem      | Broker Code |    Broker Name    | 2012 FYA | 2013 FYA | 1Q 2014A  | 2Q 2014A |
    +-----------------+--------------------+-------------+-------------------+----------+----------+-----------+----------+
    | Consensus Model | Net Revenue        | ZB          | B Securities      |          |          |    204.45 |   205.00 |
    | Consensus Model | Net Revenue        | TU          | Cantor Fitzgerald |   101.33 |          |    204.45 |   201.00 |
    | Consensus Model | Cost of Goods Sold | ZB          | B Securities      |          |          |    204.45 |   205.00 |
    | Consensus Model | Cost of Goods Sold | TU          | Cantor Fitzgerald |   101.33 |          |    204.45 |   201.00 |
    +-----------------+--------------------+-------------+-------------------+----------+----------+-----------+----------+


ToPivotTable() function sample code
C#
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;
            }


Please guide me how to customize the code in the function ToPivotTable()

As a result i can use that function to get desired output.

if ToPivotTable() function code customization not possible then also tell me how to group data using LINQ to display data as i mention in the post.

Thanks
AnswerRe: C# How to convert my List<T> to Pivot datatable Pin
Richard Deeming14-Sep-21 6:56
mveRichard Deeming14-Sep-21 6:56 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
BillWoodruff14-Sep-21 19:29
professionalBillWoodruff14-Sep-21 19:29 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 21:05
Mou_kol14-Sep-21 21:05 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 22:50
Mou_kol14-Sep-21 22:50 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Richard Deeming14-Sep-21 23:15
mveRichard Deeming14-Sep-21 23:15 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 23:54
Mou_kol14-Sep-21 23:54 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Richard Deeming15-Sep-21 0:38
mveRichard Deeming15-Sep-21 0:38 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol15-Sep-21 3:49
Mou_kol15-Sep-21 3:49 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Richard Deeming15-Sep-21 4:13
mveRichard Deeming15-Sep-21 4:13 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol15-Sep-21 20:37
Mou_kol15-Sep-21 20:37 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Pete O'Hanlon15-Sep-21 6:52
mvePete O'Hanlon15-Sep-21 6:52 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
BillWoodruff15-Sep-21 8:52
professionalBillWoodruff15-Sep-21 8:52 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol15-Sep-21 20:20
Mou_kol15-Sep-21 20:20 
QuestionHow to store Pivot data into List<T> Pin
Mou_kol14-Sep-21 5:53
Mou_kol14-Sep-21 5:53 
AnswerRe: How to store Pivot data into List<T> Pin
Richard Deeming14-Sep-21 6:04
mveRichard Deeming14-Sep-21 6:04 
GeneralRe: How to store Pivot data into List<T> Pin
Mou_kol14-Sep-21 6:29
Mou_kol14-Sep-21 6:29 
GeneralRe: How to store Pivot data into List<T> Pin
Richard Deeming14-Sep-21 6:49
mveRichard Deeming14-Sep-21 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.