Programming Newbie
Programming Newbie

Reputation: 1227

Translating SQL LIKE to LINQ to SQL

I have the following SQL fragment that I cannot figure out how to translate into LINQ

(p.prioid LIKE '1%' OR p.prioid LIKE '2%' OR p.prioid LIKE '3%')

I attempted this:

(p.prioid.Contains(1) || p.prioid.Contains(2) || p.prioid.Contains(3))

but receive the following error message: "cannot convert from int to string"

I checked the table and the 1, 2, and 3 are int data types so I don't think it would work properly if I converted the ints to strings by placing quotation marks.

Upvotes: 2

Views: 141

Answers (2)

Mohammed Swillam
Mohammed Swillam

Reputation: 9242

LIKE will only work using string data provided, even if you are searching in other datatypes.

if you notice, even in SQL, you are submitting them as strings.

p.prioid LIKE '1%' OR p.prioid LIKE '2%' OR p.prioid LIKE '3%'

a quick fix will be using .ToString() and submit your query.

(p.prioid.Contains(1.ToString()) || p.prioid.Contains(2.ToString()) || p.prioid.Contains(3.ToString()))

I guess this will be using some sort of variables rather than fixed values like the example.

Upvotes: 2

bluevector
bluevector

Reputation: 3503

Try this:

(p.prioid.ToString().StartsWith("1") || p.prioid.ToString().StartsWith("2") || p.prioid.ToString().StartWith("3"))

Quick-n-dirty :)

Upvotes: 2

Related Questions