简体   繁体   中英

entity framework LIKE query

I am using EF4.1 with MySql and unable to get LIKE query working. I want to get list of products where name like app

.Where(p=>p.Name.Contains("app") returns only one product with name Apple. The generated SQL contains LOCATE operator in place of LIKE '%app%'

I tried executing sql command using SqlQuery("select * from product where name like '%@p0%'", "app") but to no avail. It does not retrun any products.

Could some please suggest how can I write LIKE for MySql db. Thanks

I do not know EF (yo no hablo Microsoft), but I bet that the wildcards on your literal are interfering with the parameter replacement.

If you try something like the following, does it help?

SqlQuery("select * from product where name like concat('%',@p0,'%')", "app")

Is "@p0" the parameter? I assume so.

Also, try:

SqlQuery("select * from product where name like '@p0'", "%app%")

Also:

SqlQuery("select * from product where name like @p0", "'%app%'")

I bet one of these will work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM