Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL "not like" operator

Tags:

sql

linq

I have a simple SQL statement.

Select distinct value from tablename where value not like '%TEST%'

How do I write this in Linq to SQL syntax.

I tried the below statement but it doesnt seem to work.

var p = (from c in tablename where !(c.value.ToUpper().Contains("%TEST%")) 
        select c.Value).Distinct().ToList()
like image 347
mahesh Avatar asked Aug 18 '10 14:08

mahesh


2 Answers

The problem is the "%" - you're looking for things which literally don't contain "%TEST%" which would probably be everything. I think you mean:

var p = (from c in tablename
         where !c.Value.ToUpper().Contains("TEST")
         select c.Value).Distinct().ToList()
like image 55
Jon Skeet Avatar answered Oct 03 '22 22:10

Jon Skeet


If you were stuck with a pattern for sql to match, you could use SqlMethods.Like

string pattern = "%TEST%";
  ...
from c in tablename
where !SqlMethods.Like(c.Value, pattern)
select c
like image 36
Amy B Avatar answered Oct 03 '22 20:10

Amy B