Using .Net, We currently have a nested list model (list within a list) that represents a grid of data,so a list of columns within a list of rows
ie:-
public class TableViewModel
{
public List<List<TableColumn>> Grid { get; set; }
}
public class TableColumn
{
public TableColumn() { }
public TableColumn(string columnHeader, string columnValue, int columnWidth, EnumColumnType columnType, string columnName)
{
this.ColumnHeader = columnHeader;
this.ColumnValue = columnValue;
this.ColumnWidth = columnWidth;
this.ColumnType = columnType;
this.ColumnName = columnName;
}
public string ColumnHeader { get; set; }
public string ColumnName { get; set; }
public string ColumnValue { get; set; }
public int ColumnWidth { get; set; }
public EnumColumnType ColumnType { get; set; }
}
This works great when returning dynamic columns from SQL, how ever,
what we are really struggling to achieve, is to now transpose this into correct JSON using
List<List<TableColumn>> lst= getlist();
return Json(lst, JsonRequestBehavior.AllowGet);
it needs to be represented as :-
[
{name: 'Moroni', age: 50},
{name: 'Tiancum', age: 43},
{name: 'Jacob', age: 27},
{name: 'Nephi', age: 29},
{name: 'Enos', age: 34}];
]
where name and age are column headers (ColumnHeader from the model) and the correspondence is the value (ColumnValue from the model)
What would the Linq be to create this to produce the correct JSON which is returned?
Much appreciated, as we've really been struggling with this..
UPDATE :- Sample data for below message
protected void Page_Load(object sender, EventArgs e)
{
List<List<test>> m = new List<List<test>>();
List<test> lt = new List<test>();
lt.Add(new test { ColumnName = "cn1", ColumnValue = "cv1" });
lt.Add(new test { ColumnName = "cn2", ColumnValue = "cv2" });
lt.Add(new test { ColumnName = "cn3", ColumnValue = "cv3" });
m.Add(lt);
lt = new List<test>();
lt.Add(new test { ColumnName = "cn12", ColumnValue = "cv12" });
lt.Add(new test { ColumnName = "cn22", ColumnValue = "cv22" });
lt.Add(new test { ColumnName = "cn32", ColumnValue = "cv32" });
m.Add(lt);
}
public class test
{
public string ColumnName { get; set; }
public string ColumnValue { get; set; }
}