Sean Gough
Sean Gough

Reputation: 1711

Partial match on integer field in LINQ query

I can get partial matches on string fields with a query like this:

employees = context.Employees
    .Where(ee => ee.LastName.Contains(text))
    .ToList();

Is there any way to do the same for integer fields? I tried converting to a string on the fly but no luck:

employees = context.Employees
    .Where(ee => ee.EmployeeID.ToString().Contains(text))
    .ToList();

Upvotes: 1

Views: 1727

Answers (2)

David Wang RJ
David Wang RJ

Reputation: 11

I don't think this is a very good idea at all if you need to search like this you might as well have the searched column as a string. Otherwise what you are doing here is a table scan, regardless of your field is indexed. By doing so, as your table grows, the table scan will become more expensive.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500805

Well given that this is hypothetical, if EF doesn't support it directly, just force it to happen in-process:

employees = context.Employees
    .AsEnumerable()
    .Where(ee => ee.EmployeeID.ToString().Contains(text))
    .ToList();

Given that it's already a bad idea, pulling all the employee data isn't so much worse ;)

Upvotes: 2

Related Questions