开发者

LINQ version of SQL's LIKE statement

I'm new at LINQ, searching the net for LINQ samples that mimic S开发者_开发知识库QL's LIKE statement doesn't satisfy myself.

What I want is producing the same query result as this SQL

SELECT * FROM table_1 WHERE column_1 LIKE '__0%'

I want to query from table_1 where column_1's third character is '0'

Is there equivalent statement in LINQ

:D thank you


You can use the SqlMethods class. It's part of System.Data.Linq (a.k.a. LINQ to SQL).

from item in db.Table1
where SqlMethods.Like(item.Column1, "__0%")
select item;


Likes are produced by following methods: StartsWith, EndsWith and Contains. Try to play with them.


In your exact case (assuming column_1 is a string):

from t in table_1
where !String.IsNullOrEmpty(t.column_1) && t.column_1.Length >= 3 && t.column_1[2] == '0'
select t;

Of course, you have the entire .NET library at your disposals and could use some sophisticated pattern matching API (like regular expressions) if you need a more general solution:

var regex = new Regex("..0.*");
var qry = from t in table_1
          where regex.Match(t.column_1).Success
          select t;


Try this (I can't test it right now):

from t in table_1
where t.column_1[2] == '0'
select t;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜