from my sql view retunrs

so in asp.net core i need to return list of rows from dal as
as

pivot data on new column and get values from value column for each year month jnum.
how to use linq query and pivot ,reflection generic code for v1 to vn
from my sql view retunrs

so in asp.net core i need to return list of rows from dal as
as

pivot data on new column and get values from value column for each year month jnum.
how to use linq query and pivot ,reflection generic code for v1 to vn
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 11, 2023, 2:42 AM EDT
// Define a class to represent the rows of the pivoted data public class PivotedData { public string YearMonth { get; set; } public int JNum1 { get; set; } public int JNum2 { get; set; } // Add more properties for each value column } // Get the data from your DAL var data = dal.GetData(); // Use reflection to get the list of value column names var valueColumns = data.Columns.Cast() .Where(c => c.ColumnName.StartsWith("JNum")) .Select(c => c.ColumnName) .ToList(); // Use LINQ to group the data by YearMonth var groupedData = data.AsEnumerable() .GroupBy(row => row.Field("YearMonth")) .Select(group => { // Create a new PivotedData object for each group var pivotedRow = new PivotedData { YearMonth = group.Key }; // Use reflection to set the value of each property on the pivoted row foreach (var valueColumn in valueColumns) { var property = typeof(PivotedData).GetProperty(valueColumn); var sum = group.Sum(row => row.Field(valueColumn)); property.SetValue(pivotedRow, sum); } return pivotedRow; }) .ToList();