1

I'm am trying to use SQLClient for Entity Framework functions, but I'm having some issues. I need to convert ID (which in the database is a an int) to a nvarchar so that I can compare it using wildcards.

This works to an extent (i.e. it builds and executes without erroring), however it's not what I need to do as db side, it returns the wrong results. In fact, it returns 0 rows, when it should be returning a number.

return "SqlServer.STR(ID) LIKE '824%'";

The above line basically translates to the below line in SQL

SELECT COUNT(*) FROM Table1 WHERE STR(ID) LIKE '824%' 

What I need in SQL is the following line (or something similar) because this returns the correct number of rows.

SELECT COUNT(*) FROM Table1 WHERE CONVERT(NVARCHAR(50), ID) LIKE '824%'

I've tried using:

return "CAST(ID AS NVARCHAR(50)) LIKE '824%'";

But this gives the following error at runtime:

Type 'NVARCHAR' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly.

Can anyone tell me how to do this using SqlClient string functions, or some other variant?

Thanks all.

3
  • 1
    Could you change your like to '% 824%' ? Commented Oct 9, 2013 at 18:18
  • Yes, this does appear to work, many thanks. Can I ask why exactly it works like that? Thanks again. If you add this to the answers, I will mark it as answered. Commented Oct 9, 2013 at 18:31
  • Maybe you should store the data in a way that makes complex calculations on it unnecessary. If you insist, I have provided an answer. Commented Oct 9, 2013 at 19:22

1 Answer 1

2

You can using the SqlFunction.StringConvert() function. There is no overload for int, so you have to type cast

var test = dataContext.Table1
                      .Where(f => SqlFunctions.StringConvert((double) f.Id)
                                               .Trim()
                                               .StartsWith("824"))
                      .Select(f => SqlFunctions.StringConvert((double) f.Id)
                                               .Trim())
                      .ToList();

This will be converted as

SELECT 
LTRIM(RTRIM(STR( CAST( [Extent1].[Id] AS float)))) AS [C1]
FROM [dbo].[Table1] AS [Extent1]
WHERE (LTRIM(RTRIM(STR( CAST( [Extent1].[Id] AS float)))) LIKE N'4%')
Sign up to request clarification or add additional context in comments.

6 Comments

I consider this to be a hack. The problem is the right-aligning, not that the search condition is wrong. I don't like to plaster fixed over bugs, I fix the root cause. Get rid of the right-aligning (which is crazy behavior).
But using SqlFunctions.StringConvert((double)x.Id)) will not solve it because EntityFramwork is using STR to convert it.
I fail to find any way clean to convert an int into a string with EF. This is very disappointing.
actually .Trim() works. Thanks for your comment about it being a hack It forced me to dung further. I'll update my anwsers.
I'm forced to upvote this disgusting hack. I'd probably investigate custom EF functions. There seems to be a way to reference T-SQL functionality and casts.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.