Click here to Skip to main content
15,351,089 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff15-Sep-21 22:22
mveBillWoodruff15-Sep-21 22:22 
GeneralRe: How to convert Persian date representation? Pin
Alex Dunlop16-Sep-21 1:11
MemberAlex Dunlop16-Sep-21 1:11 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming16-Sep-21 1:26
mveRichard Deeming16-Sep-21 1:26 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff16-Sep-21 4:23
mveBillWoodruff16-Sep-21 4:23 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming16-Sep-21 4:53
mveRichard Deeming16-Sep-21 4:53 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff16-Sep-21 5:01
mveBillWoodruff16-Sep-21 5:01 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff21-Sep-21 8:52
mveBillWoodruff21-Sep-21 8:52 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming21-Sep-21 21:45
mveRichard Deeming21-Sep-21 21:45 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff21-Sep-21 23:21
mveBillWoodruff21-Sep-21 23:21 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming21-Sep-21 23:24
mveRichard Deeming21-Sep-21 23:24 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff22-Sep-21 0:53
mveBillWoodruff22-Sep-21 0:53 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff16-Sep-21 1:28
mveBillWoodruff16-Sep-21 1:28 
QuestionC# Confusion about LINQ Grouping Pin
Mou_kol14-Sep-21 23:11
MemberMou_kol14-Sep-21 23:11 
AnswerRe: C# Confusion about LINQ Grouping Pin
Richard Deeming14-Sep-21 23:42
mveRichard Deeming14-Sep-21 23:42 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol14-Sep-21 23:51
MemberMou_kol14-Sep-21 23:51 
GeneralRe: C# Confusion about LINQ Grouping Pin
Richard Deeming15-Sep-21 0:41
mveRichard Deeming15-Sep-21 0:41 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 3:51
MemberMou_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
mveBillWoodruff15-Sep-21 19:35 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 20:22
MemberMou_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
MemberMou_kol16-Sep-21 6:30 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 20:39
MemberMou_kol15-Sep-21 20:39 
QuestionC# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 6:00
MemberMou_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 

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.