I have a service developed with WCF
, in this service I call a stored procedure
, it returns 4 fields: id
, id_visita
, fecha
, observaciones
. It may be the case that the field id_visita
is null and this is because the corresponding visit has not been made, therefore the value fecha
is from a day before today and the field observaciones
is also null.
In my service I do the following:
int gestion_ruta_visita_id = Convert.ToInt32(dr["id_visita"]);
if (gestion_ruta_visita_id != 0)
{
//Si es que ya fue visitada, obtener detalles de la visita
gestionRuta.estado = 1;
gestionRuta.gestion_ruta_visita_fecha = Convert.ToDateTime(dr["fecha"]).ToString("dd-MM-yyyy HH:mm");
gestionRuta.gestion_ruta_visita_observaciones = Convert.ToString(dr["observaciones"]);
}
In this code I obtain whether or not it has the id_visita
, if it is different from 0, it means that the visit has already been carried out, therefore my object gestionRuta
has the following:
[DataMember]
public string gestion_ruta_visita_fecha { get; set; }
[DataMember]
public string gestion_ruta_visita_observaciones { get; set; }
These fields are ONLY filled when the id_visita
is different from 0, but in the other case, that is 0, I don't want them to appear as null in my json, just not to appear.
Current result :
"rutas": [
{
"estado": 1,
"gestion_ruta_codigo_sala": "3200007286",
"gestion_ruta_direccion_sala": "La Florida",
"gestion_ruta_latitud_sala": "-33.4141632",
"gestion_ruta_longitud_sala": "-70.627686",
"gestion_ruta_nombre_sala": "Jumbo La Florida",
"gestion_ruta_programacion_fecha": "25-05-2015",
"gestion_ruta_programacion_id": 391,
"gestion_ruta_visita_fecha": "02-06-2016 16:11",
"gestion_ruta_visita_observaciones": "Sin observaciones"
},
{
"estado": 2,
"gestion_ruta_codigo_sala": "3200007286",
"gestion_ruta_direccion_sala": "La Florida",
"gestion_ruta_latitud_sala": "-33.4141632",
"gestion_ruta_longitud_sala": "-70.627686",
"gestion_ruta_nombre_sala": "Jumbo La Florida",
"gestion_ruta_programacion_fecha": "27-05-2015",
"gestion_ruta_programacion_id": 392,
"gestion_ruta_visita_fecha": null,
"gestion_ruta_visita_observaciones": null
},
Expected result :
"rutas": [
{
"estado": 1,
"gestion_ruta_codigo_sala": "3200007286",
"gestion_ruta_direccion_sala": "La Florida",
"gestion_ruta_latitud_sala": "-33.4141632",
"gestion_ruta_longitud_sala": "-70.627686",
"gestion_ruta_nombre_sala": "Jumbo La Florida",
"gestion_ruta_programacion_fecha": "25-05-2015",
"gestion_ruta_programacion_id": 391,
"gestion_ruta_visita_fecha": "02-06-2016 16:11",
"gestion_ruta_visita_observaciones": "Sin observaciones"
},
{
"estado": 2,
"gestion_ruta_codigo_sala": "3200007286",
"gestion_ruta_direccion_sala": "La Florida",
"gestion_ruta_latitud_sala": "-33.4141632",
"gestion_ruta_longitud_sala": "-70.627686",
"gestion_ruta_nombre_sala": "Jumbo La Florida",
"gestion_ruta_programacion_fecha": "27-05-2015",
"gestion_ruta_programacion_id": 392
},
I have found a solution
Define as
DataContract
my class and the elements that I don't want it to return when they are null as[DataMember(EmitDefaultValue = false)]
If there is another better solution, I'll wait for it :D