Not like LINQ to SQL

I need to select data from a database using LINQ to SQL. There is a condition that I should select only the record with the identifier not containing “0000” at the beginning (the entire identification number has six digits).

For example, when I want to select data starting with "0000", I will use:

var idList = (from s in db.TABLE where s.ID.StartsWith("0000") select s.ID ); 

but I need to use the method more like NotStartsWith or NotContains instead of StartsWith . Is it possible?

+6
c # linq-to-sql
source share
5 answers

Have you tried !s.ID.StartsWith("0000") ? (i.e. using no operator ! )

+6
source share
 var idList = (from s in db.TABLE where !s.ID.StartsWith("0000") select s.ID ); 
+3
source share

Startswith returns a boolean value. That way you can simply negate StartsWith. Your request should look like this:

 var idList = (from s in db.TABLE where !s.ID.StartsWith("0000") select s.ID ); 
+3
source share

Of course .. just add the logical negation operator (!) Before the StartsWith status:

! S.ID.StartsWith ("0000")

+2
source share

This is a convenient extension method.

 public static class StringExtenstionMethods { public static bool DoesNotStartWith(this string source,string target) { return !source.StartsWith(target); } } var idList = (from s in db.TABLE where s.ID.DoesNotStartWith("0000") select s.ID); 
0
source share

All Articles