开发者

Partial match on integer field in LINQ query

开发者 https://www.devze.com 2023-03-16 09:30 出处:网络
I can get partial matches on string fields with a query like this: employees = context.Employees .Where(ee => ee.LastName.Contains(text))

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 开发者_如何学JAVAno luck:

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


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 ;)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消