Integer Contains Using Linq

c# casting entity-framework linq

Question

I'm having some difficulty writing a linq query that will check whether the consecutive digits in an integer are contained in the primary key of a table. So, suppose there is a table called Employees with a primary key on the column Employees.Id. Suppose this primary key is of Sql Server datatype INT. I would like to write a linq query using Entity Framework Code First that will return all employees whose primary key contains the string 456. Something like:

string filter = "456";

var results = from e in myDbContext.Employees
  where e.Id.Contains(filter)
  select e;

The problem is that the Contains method is not offered for integer datatypes in C#...

1
13
10/6/2015 10:40:13 AM

Accepted Answer

Try:

var results = from e in myDbContext.Employees
  where SqlFunctions.StringConvert((double)e.Id).Contains(filter)
  select e;
28
6/14/2013 2:36:53 PM

Popular Answer

You can convert both to string and then do the query. In your case:

string filter = "456"; var results = from e in myDbContext.Employees where e.Id.ToString().Contains(filter) select e;



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow