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

后端 未结 1 1005
耶瑟儿~
耶瑟儿~ 2020-12-16 13:13

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.         


        
相关标签:
1条回答
  • 2020-12-16 13:36

    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 讨论(0)
提交回复
热议问题