Hi i want compare IQueryable to "[ ]" because is what API returns when is null
i have this
var query = from Consumer in context.Consumer
from Patient in context.Patient
where Consumer.id == iduser
where Patient.PATHOLOGY_id == idpato
where Consumer.id == Patient.CONSUMER_id_patient
select Patient;
if ( query == "[]")
{
return this.RedirectToAction(nameof(this.PatientCreate));
}
else
{
return Ok(query.ToString());
}
but i can't use operattorers on IQueryable, how i do that?
Your query will "return" a collection of Patient
objects (technically a lazy-loaded iterator, but functionally a collection), so it will not ever "equal" the string "[]". If the response from the data source is an empty JSON list, then query
will most likely represent an empty collection, so you could try:
if(!query.Any())
// no results
else
// do something
But note that query.Any()
will execute the query, and iterating the results in the view will also run the query, so you probably want to execute the query only once - ToList()
is an effective way to do that:
var query = (from Consumer in context.Consumer
from Patient in context.Patient
where Consumer.id == iduser
where Patient.PATHOLOGY_id == idpato
where Consumer.id == Patient.CONSUMER_id_patient
select Patient).ToList();
That way, you've already collected the results, so Any()
will not
Finally, query.ToString()
will not do what you probably think it does - I presume that you just want to pass the collection to the view, so you probably just want
return Ok(query);
It seems that you need something like this:
var query = from Consumer in context.Consumer
from Patient in context.Patient
where Consumer.id == iduser
where Patient.PATHOLOGY_id == idpato
where Consumer.id == Patient.CONSUMER_id_patient
select Patient;
var result = query.ToArray();
if ( !result.Any())
{
return this.RedirectToAction(nameof(this.PatientCreate));
}
else
{
return Ok(result);
}
var query = from Consumer in context.Consumer
from Patient in context.Patient
where Consumer.id == iduser
where Patient.PATHOLOGY_id == idpato
where Consumer.id == Patient.CONSUMER_id_patient
select Patient;
if ( query.Count() == 0 )
{
return this.RedirectToAction(nameof(this.PatientCreate));
}
else
{
return Ok(query.ToString());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.