Linq to Entities (EF 4.1): How to do a SQL LIKE with a wildcard in the middle ( '%term%term%')?

后端 未结 3 1573
陌清茗
陌清茗 2021-01-01 16:04

I want to search for this:

Post Cereal

and get this:

Post Honey Nut Cereal

where the wild cards would be

3条回答
  •  青春惊慌失措
    2021-01-01 16:23

    Just to clarify Ladislav's comment regarding it.BusinessName. I think what he is referring to is prefixing the field name with .it. The above solution works as long as you prefix the field name in the where clause with it.. Also I didn't need the TRIM() in my case.

    var query - db.table.Where("it.fieldname LIKE @pattern");
    query.Parameters.Add(new ObjectParameter("pattern", term)); // term == "%what%ever%"
    

    It worked perfectly against an Oracle database.

提交回复
热议问题