Description:
http://stackoverflow.com/questions/27338108/mysql-linq-using-containsvariable
Setup info:
VS2013 / C#
EF6
MySQL database
.Net Connector 6.9.5
I'm trying to create a method that returns a collection of Account records using a partial name as the search criteria. If I hard code a string value using the IQueryable .Contains() extension method, it returns data. However, when I attempt to use a variable no data is returned.
Public class Test() {
MyEntities db = new MyEntities();
//Works....but the search criteria is hard coded.
public IQueryable<Account> WorksButValueHardCoded() {
return (from a in db.Accounts
where a.accountname.Contains("Test")
select a);
}
//Does not return anything
public IQueryable<Account> DoesNotReturnAnyData() {
//Obviously I would use a parameter, but even this test fails
string searchText = "Test";
return (from a in db.Accounts
where a.accountname.Contains(searchText)
select a);
}
}
How to repeat:
Create a context
query that context and pass a variable to the the .Contains extension method for a VARCHAR field.
Suggested fix:
I can see in the LINQ generated SQL used the LIKE operator, but I don't understand how the variable is injected to suggest a fix.
SELECT
`Extent1`.`accountid`,
`Extent1`.`accountname`
FROM `account` AS `Extent1`
WHERE `Extent1`.`accountname` LIKE '%p__linq__0%'