I'm trying to make a query through a GET request (in Angular.js) using .NET Web API and Entity Framework using LINQ and MySQL db.
http://localhost/ProductsApp/api/clientes/GetByName/M
The problems is that the parameter passed by GET is not evaluated and I don't get any results (even though name is being set correctly to "M" according to the debugger).
However, if I hardcode a string, I get the expected result.
[ActionName("GetByName")]
public IEnumerable<cliente> GetByName(string name)
{
var query = from c in context.clientes where c.nome.StartsWith(name) select c;
var query2 = from c in context.clientes where c.nome.StartsWith("M") select c;
var query3 = context.clientes.Where(c => c.nome.StartsWith(name));
var query4 = context.clientes.Where(c => c.nome.StartsWith("M"));
return query.ToList();
}
My WebApiConfig.cs has the following lines:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null
);
I suppose that LINQ resolves the variables when it is executed, as hinted below (I removed the unimportant part of the query):
query.ToString()
"SELECT [...] WHERE `Extent1`.`nome` LIKE 'p__linq__0%'"
query2.ToString()
"SELECT [...] WHERE `Extent1`.`nome` LIKE 'M%'"
query3.ToString()
"SELECT [...] WHERE `Extent1`.`nome` LIKE 'p__linq__0%'"
query4.ToString()
"SELECT [...] WHERE `Extent1`.`nome` LIKE 'M%'"
Both query2 and query4 return correct values while query and query3 do not. Why? How can I make it work?
even though name is being set correctly to "M"You seem to have some pretty compelling evidence that that isn't the case. How do you know thatnameis being set toM? - Servy"SELECT\r\nExtent1.cliente_id, \r\nExtent1.nome, \r\nExtent1.email, \r\nExtent1.endereco\r\nFROMcliente` ASExtent1\r\n WHEREExtent1.nomeLIKE 'p__linq__0%'"` - jsallaberry