Click here to Skip to main content
15,311,014 members
Home / Discussions / C#
   

C#

 
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 
This is my data on which a LINQ query executed. i have bit confusion about LINQ grouping used there. here is sample code.

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"
 },
};

var periods = _data.Select(y => y.Period).Distinct().OrderBy(y => y).ToArray();

var results =
 _data
	 .GroupBy(
		 x => new { x.Section, x.Lineitem, x.BrokerCode, x.BrokerName },
		 x => new { x.Period, x.PeriodValue })
	 .Select(x => new 
	 {
		 x.Key,
		 Lookup = x.ToLookup(y => y.Period, y => y.PeriodValue),         
	 })
	 .Select(x => new
	 {
		 x.Key.Section,
		 x.Key.Lineitem,
		 x.Key.BrokerCode,
		 x.Key.BrokerName,
		 Map = periods.ToDictionary(y => y, y => String.Join("|", x.Lookup[y])),
	 })
	 .ToArray();
	
var dt = new DataTable();
dt.Columns.Add("Section");
dt.Columns.Add("Lineitem");
dt.Columns.Add("BrokerCode");
dt.Columns.Add("BrokerName");
foreach (var period in periods)
{
 dt.Columns.Add(period);
}

foreach (var result in results)
{
 string[] key_values = new string[]
 {
	 result.Section,
	 result.Lineitem,
	 result.BrokerCode,
	 result.BrokerName,
 };
	
 string[] period_values = periods.Select(p => result.Map[p]).ToArray()
	
 dt.Rows.Add(key_values.Concat(period_values).ToArray());
}


i have to group on few fields and those are Section, LineItem, BrokerCode and Period but a guy does the grouping like this way

C#
var results =
 _data
	 .GroupBy(
		 x => new { x.Section, x.Lineitem, x.BrokerCode, x.BrokerName },
		 x => new { x.Period, x.PeriodValue })
	 .Select(x => new 
	 {
		 x.Key,
		 Lookup = x.ToLookup(y => y.Period, y => y.PeriodValue),         
	 })
	 .Select(x => new
	 {
		 x.Key.Section,
		 x.Key.Lineitem,
		 x.Key.BrokerCode,
		 x.Key.BrokerName,
		 Map = periods.ToDictionary(y => y, y => String.Join("|", x.Lookup[y])),
	 })
	 .ToArray();


So my question is what kind of grouping is it.... see the below grouping code.
C#
.GroupBy(
 x => new { x.Section, x.Lineitem, x.BrokerCode, x.BrokerName },
 x => new { x.Period, x.PeriodValue })


Is it two set of grouping ? one set is { x.Section, x.Lineitem, x.BrokerCode, x.BrokerName }
and another set is { x.Period, x.PeriodValue } ?

please guide me how the above grouping will be working ?

Thanks
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 
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 
GeneralRe: C# How to convert my List<T> to Pivot datatable Pin
Mou_kol14-Sep-21 23:54
MemberMou_kol14-Sep-21 23:54 

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.