Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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#...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
251 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...