I get below data from Postgresql table, need to sum the ListDate field for each month and transpose.
I tried this link Transpose a datatable using linq query , but not feasible or right approach to achieve it.
ListDate | MM | FC | AMS | KS |
---|---|---|---|---|
2023-01-01 | 12 | 13 | 34 | 26 |
2023-01-01 | 22 | 23 | 44 | 46 |
2022-12-01 | 32 | 13 | 34 | 26 |
2023-12-01 | 42 | 13 | 64 | 16 |
2023-11-10 | 62 | 13 | 94 | 36 |
2023-11-23 | 02 | 13 | 34 | 46 |
Expected Result –
. | JAN-23 | DEC-22 | NOV-22 |
---|---|---|---|
MM | 34 | 74 | 64 |
FC | 36 | 26 | 26 |
AMS | 78 | 98 | 118 |
KS | 72 | 42 | 82 |
2
Answers
this is my class
The first point:
12-01-2023
should be2022-12-01
in order, the question data to match theExpected Result
.If we have the following list:
We can use
Substring(0,7)
to group by year and month.Now the key of group by
year-month(ex: 2023-12)
. We can get the month name usingDateTimeFormat.GetMonthName
and calculate the
last two digits of the year using the substring(2,2)
And finally,
collect the parameters
andconvert them into a list
Result: