开发者

How do I query an integer column for "starts with" in Entity Framework?

开发者 https://www.devze.com 2023-04-12 05:22 出处:网络
I have a co开发者_如何转开发lumn that\'s defined as an integer in EF (Code First).I want to search it using \"starts with.\" Now, I can do this:

I have a co开发者_如何转开发lumn that's defined as an integer in EF (Code First). I want to search it using "starts with." Now, I can do this:

Where(x => SqlFunctions.StringConvert((double)x.AccountNumber).StartsWith(searchTerm))

However, SqlFunctions.StringConvert() gets translated to the T-SQL function STR(), which left-pads the result for reasons which are beyond my comprehension.

Also, I can't use string.TrimStart() because it's not supported by the Entity Framework.

Can anyone lend any help?


Trim() and TrimStart() work in LINQ to Entities, so you can use:

Where(x => SqlFunctions.StringConvert((double)x.AccountNumber)
    .TrimStart().StartsWith(searchTerm))

TrimStart translates into LTRIM in SQL. With searchTerm = 123 for example you get something like:

WHERE LTRIM(STR( CAST( [Extent1].[AccountNumber] AS float))) LIKE N'123%'
0

精彩评论

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

关注公众号