I have a basic linq that gets 10 rows from database; get 2 columns (First_Name & Last_Name) from database. – this works ok
Question – how can i add column names at 1st row of list?
var query = _context1.dbset
.Select(x =>
new
{
First_Name = x.First_Name,
Last_Name = x.Last_Name
}).Take(10).ToList();
What I tried. Below works good but I have to manually type all column names and also when adding data in for loop
`
List<object> chartData = new List<object>();
data .Add(new object[] { "First_Name", "Last_Name" });
foreach (var item in query)
{
Data.Add(new object[] { item.First_Name, item.Last_Name });
}
2
Answers
Use reflection:
you can use this code