如何查询“开头为"的整数列?在实体框架中? [英] How do I query an integer column for "starts with" in Entity Framework?
问题描述
我有一列在 EF(代码优先)中定义为整数.我想使用开始于"来搜索它.现在,我可以这样做:
I have a column 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))
然而,SqlFunctions.StringConvert()
被转换为 T-SQL 函数 STR()
,由于我无法理解的原因,它对结果进行了左补.
However, SqlFunctions.StringConvert()
gets translated to the T-SQL function STR()
, which left-pads the result for reasons which are beyond my comprehension.
此外,我不能使用 string.TrimStart()
,因为实体框架不支持它.
Also, I can't use string.TrimStart()
because it's not supported by the Entity Framework.
有人可以帮忙吗?
推荐答案
Trim()
和 TrimStart()
在 LINQ to Entities 中工作,因此您可以使用:
Trim()
and TrimStart()
work in LINQ to Entities, so you can use:
Where(x => SqlFunctions.StringConvert((double)x.AccountNumber)
.TrimStart().StartsWith(searchTerm))
TrimStart
在 SQL 中转换为 LTRIM
.例如,使用 searchTerm
= 123,你会得到如下结果:
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%'
这篇关于如何查询“开头为"的整数列?在实体框架中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!