Iulian
Iulian

Reputation: 1200

linq to sql wildcard on numeric column

In SQL i can do something like this :

select  * from Table1 where Col1 like '%'

I'm trying to this in linq like this :

from p in Table1
where SqlMethods.Like(p.Col1, "%")
select  new { p.Col1}

But it's not working because it's expecting Col1 to be int.

The reason i'm doing this is because i'm trying to build a search form and must use wildcards for the parameters that aren't completed (some are strings, some are numeric).

Upvotes: 0

Views: 1220

Answers (3)

Simeon
Simeon

Reputation: 5579

Hm. Like this?

from p in Table1
where p.Col1.ToString().Contains("foo")
select new { p.Col1}

Exchange Contains with BeginsWith/StartsWith as the LIKE functionality can... or use RegExp if it's a more complex expression.

Upvotes: 0

smartcaveman
smartcaveman

Reputation: 42276

How about, for integer values, you specify a range and a predicate that specifies the value must be greater than minimum/less than maximum instead? This seems more intuitive to me than doing a string comparison on a numeric field.

Upvotes: 3

Keith
Keith

Reputation: 5391

Why not try converting to string then?

from p in Table1
where SqlMethods.Like(p.Col1.ToString(), "%")
select  new { p.Col1}

Upvotes: 2

Related Questions