I'm taking a datatable and serializing it as geojson. I'm using linq for this:
var envelope = new
{
type = "FeatureCollection",
features = dataTable.AsEnumerable().Select(record => new {
type = "Feature",
properties = new
{
Name = Convert.ToString(record["Name"]),
Date = Convert.ToString(record["Date"]),
Icon = Convert.ToString(record["imageUrl"]),
//ReportMonth = Convert.ToString(record["Month"]),
ReportMonth = (!string.IsNullOrEmpty(record["Month"])) ? Convert.ToString(record["ReportMonth"]) : string.Empty
},
geometry = new
{
type = "Point",
coordinates = new[] {
Convert.ToDecimal(record["Lon"]),
Convert.ToDecimal(record["Lat"])
}
}
}).ToArray()
};
This works when the datatable has all the columns. When the column doesn't exist in the datatable (ie. column Month
) then the iteration fails.
Is there a way to check if the column exists? I tried using a ternary operator to check the value, but it obviously won't work since I'm still checking if the value exists.