Click here to Skip to main content
15,312,257 members
Home / Discussions / C#
   

C#

 
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 
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
mveBillWoodruff14-Sep-21 19:29 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 21:05
MemberMou_kol14-Sep-21 21:05 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 22:50
MemberMou_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 
ITuple was added in .NET 4.7.1; you will need to upgrade your project to at least that version to use it.

NB: .NET Framework 4.5.2 will reach end of support next April:
.NET Framework 4.5.2, 4.6, 4.6.1 will reach End of Support on April 26, 2022[^]

If you want a solution for an earlier version of .NET, then you'll need to write a specific method for your data - eg:
C#
public static DataTable DataToPivotTable(this IEnumerable<Data> source)
{
    var result = new DataTable();
    result.Columns.Add(new DataColumn("Section", typeof(string)));
    result.Columns.Add(new DataColumn("Lineitem", typeof(string)));
    result.Columns.Add(new DataColumn("BrokerCode", typeof(string)));
    result.Columns.Add(new DataColumn("BrokerName", typeof(string)));
    
    var sourceList = source.ToList();
    var periods = sourceList.Select(d => d.Period).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
    foreach (string period in periods)
    {
        result.Columns.Add(new DataColumn(period, typeof(string)));
    }
    
    var groupedData = source.GroupBy(d => new
    {
        d.Section,
        d.Lineitem,
        d.BrokerCode,
        d.BrokerName
    });
    
    foreach (var group in groupedData)
    {
        var row = result.NewRow();
        row["Section"] = group.Key.Section;
        row["Lineitem"] = group.Key.Lineitem;
        row["BrokerCode"] = group.Key.BrokerCode;
        row["BrokerName"] = group.Key.BrokerName;
        
        foreach (var item in group)
        {
            row[item.Period] = item.PeriodValue;
        }
        
        result.Rows.Add(row);
    }
    
    return result;
}
C#
DataTable pivotedData = _data.DataToPivotTable();
Output is identical to the previous method, but you won't be able to reuse this method for other input types or other sets of columns.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 23:54
MemberMou_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
MemberMou_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
MemberMou_kol15-Sep-21 20:37 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Pete O'Hanlon15-Sep-21 6:52
mvaPete O'Hanlon15-Sep-21 6:52 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
BillWoodruff15-Sep-21 8:52
mveBillWoodruff15-Sep-21 8:52 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol15-Sep-21 20:20
MemberMou_kol15-Sep-21 20:20 
QuestionHow to store Pivot data into List<T> Pin
Mou_kol14-Sep-21 5:53
MemberMou_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
MemberMou_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 
GeneralRe: How to store Pivot data into List<T> Pin
Mou_kol14-Sep-21 23:14
MemberMou_kol14-Sep-21 23:14 
GeneralRe: How to store Pivot data into List<T> Pin
Richard Deeming14-Sep-21 23:17
mveRichard Deeming14-Sep-21 23:17 
QuestionActive step-two verfication telegram with c# Pin
Nader Rostamkhani8-Sep-21 6:22
MemberNader Rostamkhani8-Sep-21 6:22 
Rant[REPOST] Active step-two verfication telegram with c# Pin
Richard Deeming8-Sep-21 6:33
mveRichard Deeming8-Sep-21 6:33 
QuestionWhat is default unit of EPPlus (point, inches, pixel) Pin
Mou_kol8-Sep-21 4:39
MemberMou_kol8-Sep-21 4:39 

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.