I have 1 class like below:
public class Tables
{
public string Name { get; set; }
public string[] Columns { get; set; }
}
string[] selectedTables = { "Table1", "Table2"};
using (var conection = new SqlConnection("MyconnectionString"))
{
connection.Open();
var tables = (
from table in connection.GetSchema("Tables").AsEnumerable()
let name = (string)table["TABLE_NAME"]
where selectedTables.Contains(name)
let catalog = (string)table["TABLE_CATALOG"]
let schema = (string)table["TABLE_SCHEMA"]
select new Tables // this should really be called Table
{
Name = name,
Columns = (
from column in connection.GetSchema("Columns", new [] { catalog, schema, name }).AsEnumerable()
select (string)column["COLUMN_NAME"]).ToArray()
}).ToList();
return tables;
}
Here i am creating response to return data to service:
var response = tables.
Select
(
t => new
{
id = t.Id,
tables = t.Tables
.Select
(
x => new
{
name = x.Name,
columns = x.Columns
}
).ToList()
}
).ToList();
return Json(response);
Below is my JSON:
Now when generating columns data for each tables if columns is null for suppose Table1 then I don't want this columns field to be present in my JSON.
Is this possible because I follow this Question in which it is written that it is not possible.
Update: I am using asp.net mvc so I am doing like this to create json - is that possible?
return Json(response);
This is not duplicate question as I am using JSON class of MVC to generate JSON result.