Kumar S

Kumar S

  • NA
  • 110
  • 2.4k

Unpivot multiple columns

Apr 30 2020 8:58 AM
I'm a SQL developer and little exp on c# . I have unpivot result using the SQL query.
I wanted to give a try in C#
I need help to unpivot the list in the below format
 
public class TDResponse
{
public string CenterID { get; set; }
public string Center { get; set; }
public string Yr { get; set; }
public string Mth { get; set; }
public string Test_S { get; set; }
public string Test_U { get; set; }
public string Dev_S { get; set; }
public string Dev_U { get; set; }
}
private static List<TDResponse> CreateDatSample()
{
List<TDResponse> lst = new List<TDResponse>();
lst.Add(new TDResponse { CenterID = "491", Center = "Enterprise", Yr = "2020", Mth = "APRIL", Test_S = "0", Test_U = "3", Dev_S = "45", Dev_U = "0" });
lst.Add(new TDResponse { CenterID = "491", Center = "Enterprise", Yr = "2020", Mth = "JULY", Test_S = "0", Test_U = "35", Dev_S = "21", Dev_U = "0" });
lst.Add(new TDResponse { CenterID = "491", Center = "Enterprise", Yr = "2020", Mth = "JUNE", Test_S = "0", Test_U = "43", Dev_S = "34", Dev_U = "0" });
lst.Add(new TDResponse { CenterID = "491", Center = "Enterprise", Yr = "2020", Mth = "May", Test_S = "0", Test_U = "311", Dev_S = "55", Dev_U = "0" });
return lst;
}
 
Output needed
Test_S / Test_U to be coded as Test
Values of the both columns into SVal and UVal
 
CenterID | Center | Code | SVal | UVal | Yr | Mth
------+-------+-------+------+-------+-------------+-------+-------
491 | Enterprise | Test | 0    | 3       | 2020 | APRIL
491 | Enterprise | Test | 0    | 311   | 2020 | May
491 | Enterprise | Test | 0    | 43     | 2020 | JUNE
491 | Enterprise | Test | 0    | 35     | 2020 | JULY
491 | Enterprise | Dev | 45 | 0        | 2020 | APRIL
491 | Enterprise | Dev | 55 | 0        | 2020 | May
491 | Enterprise | Dev | 34 | 0        | 2020 | JUNE
491 | Enterprise | Dev | 21 | 0        | 2020 | JULY

Answers (4)