开发者

Not like in LINQ to SQL

开发者 https://www.devze.com 2023-02-04 01:06 出处:网络
I have to select data from database using LINQ to SQL. 开发者_运维百科There is a condition that i should select only record with ID not containing \"0000\" at begin ( whole ID number have six digits).

I have to select data from database using LINQ to SQL. 开发者_运维百科There is a condition that i should select only record with ID not containing "0000" at begin ( whole ID number have six digits).

For example when I would 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 method more like NotStartsWith or NotContains instead of StartsWith. Is that possible?


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


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


Startswith returns an boolean value. So you can simply negate the StartsWith. Your query should be like below;

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


Of Course.. just add a logical negation operator (!) in front of the StartsWith statetment:

!s.ID.StartsWith("0000")


Heres a handy 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

精彩评论

暂无评论...
验证码 换一张
取 消