strike_noir
strike_noir

Reputation: 4174

LINQ version of SQL's LIKE statement

I'm new at LINQ, searching the net for LINQ samples that mimic SQL'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

Upvotes: 9

Views: 2427

Answers (4)

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

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

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

Upvotes: 0

Steven
Steven

Reputation: 172646

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;

Upvotes: 11

Isak Savo
Isak Savo

Reputation: 35884

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;

Upvotes: 0

Andrey
Andrey

Reputation: 60065

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

Upvotes: 12

Related Questions