LINQ2SQL LIKE Command for Phrase

后端 未结 3 1210
傲寒
傲寒 2020-12-20 17:47

I am trying to retrieve a list of string where it contains \"Britney Spears\", and this is what I use

from p in Objects
where p.Title.Contains(\"Britney Spea         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 18:06

    do exactly as you would with straight up SQL

    you can supply special characters with the like clause such as ".", "%" and "[]"

    if that is not good enough, then you can use regex, but be careful since regex will be executed "client"-side, i.e. it will fetch the whole thing and then try to narrow results down in memory. You can somewhat optimize zerkms' method by doing

    from p in Objects
    where p.Title.Contains("Britney") && p.Title.Contains("Spears") && regex.Match(p.Title).Success
    select p
    

    I haven't tested it, but it should run first two conditions on the SQL server side, and then narrow down using regex.

提交回复
热议问题