Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

user trimstart in entity framework query

How can I use trimstart so entity framework will understand what to do?

Here is my query:

string number="123";
Workers.Where(x => x.CompanyId == 8).Where(x => x.Number.TrimStart('0') == number);

How can I make this query work without the AsEnumerable (there are a lot of workers in company 8)?

like image 730
Naor Avatar asked Oct 29 '11 02:10

Naor


1 Answers

Try using SqlFunctions.PatIndex for this. I tested a query similar to the one below with the values "000123", "000One", "abcde" and it correctly selected rows with the values "123", "One", and "abcde".

Workers.Where(x => x.CompanyId == 8 && 
                   x.Number.Substring(SqlFunctions.PatIndex("%[^0]%", x.Number).Value - 1) == number);
like image 129
Jeff Ogata Avatar answered Oct 15 '22 03:10

Jeff Ogata