3
I have a action [GET] delete
on my Webapi.
She removes one element, but would like it to remove all the elements that have a particular code.
How can I change my code to remove all?
My Code:
[HttpGet]
[Route("delete/{Con_codigo}")]
public HttpResponseMessage delete(int Con_codigo)
{
try
{
var condel = bdprincipalEntities.Contatos
.FirstOrDefault(x => x.Con_codigo == Con_codigo);
bdprincipalEntities.Contatos.Remove(condel);
bdprincipalEntities.SaveChanges();
var result = new HttpResponseMessage(HttpStatusCode.OK);
return result;
}
catch (Exception)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
}
I know that my search returns only the first. I thought of changing from FirstOrDefault
for Select
, but he gives me the following error message:
Unable to convert from "System.linq.Iqueryable to Softluxapi.Models.
Updating:
The above method ends up removing all contacts, as I was asking, so I’m changing the question to, because even with the FirstOrDefault
it removes various elements from the database?
An important tip: Don’t change the focus of your question after posting it, especially if the post has already received answers. You will end up causing your question to get confused and the respondents lose the job they had to write an answer. If you have a different question and it is pertinent to ask here, open a new question, there is no limit to posts.
– Jéf Bueno