Scottie
Scottie

Reputation: 11308

How can I write this query in Linq to Entities extension method as a where clause?

Given the following SQL statement:

Select * From Customers Where ID LIKE ('%160%')

if ID is an int field, how can I use a Linq to Entities extension method to do this?

For string fields, I can do something like this...

filterExpression = "ID.Contains(\"160\")";
IQueryable<Customer> c = context.Customers.Where(filterExpression);

When I try this, I get the error:

No applicable method 'Contains' exists in type 'Int32'

I've been searching for an hour on how to get this to work with no luck.

Edit: Modifed to make the code more readable.

Upvotes: 0

Views: 158

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

Can't test atm, but I'd try this;

var myExpression = x => SqlFunctions.StringConvert((double)x.ID).Contains("160");
IQueryable<Customer> c = context.Customers.Where(myExpression);

Upvotes: 1

Related Questions