Sanish Joseph
Sanish Joseph

Reputation: 2256

Why is Linq not able to search Arabic Text?

I have an Arabic text and English text in my DB. I am trying to search from my DB based on the text user enters. It can be English or Arabic.

I am using SQL and Entity Framework and below is the code portion in which I am trying to fetch the results matching the text.

  results = results.Where(c => c.ResourceValue.Contains(AnyText));

When it's English, I am getting proper results but when it's Arabic, it returns results with "???s" in DB. Those results are garbage.

I assume the problem is that the Arabic text is being compared as "???s" and Linq to Entity returns me results with '???s'.

How can I handle it?

Update:

I have used the SQL profiler to see what query is run against the DB. As suspected, Arabic input [Search text] is not going thorough but it's gone as '???s'

exec sp_executesql N'SELECT  
[Extent1].[ResourceValue] AS [ResourceValue]  
FROM (SELECT      
[Table].[ResourceValue] AS [ResourceValue]   
FROM [dbo].[Table] AS [VpStringResource])
AS [Extent1]  WHERE ([Extent1].[ChannelID] IN (@p__linq__0,0)) AND (( CAST(LEN([Extent1].
[ResourceValue]) AS int)) <> 0) AND ([Extent1].[ResourceValue] LIKE @p__linq__1 ESCAPE 
''~'')',N'@p__linq__0 smallint,@p__linq__1 varchar(8000)',@p__linq__0=2,
@p__linq__1='%??????%'

To Reproduce the issue,

As I mentioned, you can use any sql table. Add a column of type nVarchar(max). Add some Arabic text to the table. Use Entity Framework in C# and try to use the line in the question. Like

Context.Table.Where(c => c.ResourceValue.contains("اختبار")) 

Upvotes: 4

Views: 2575

Answers (1)

Sanish Joseph
Sanish Joseph

Reputation: 2256

After a good research, I found solution for my own problem.

results = results.Where(c => c.ResourceValue.Contains(EntityFunctions.AsUnicode(AnyText)));

I had to import using System.Data.Entity.Core.Objects; also.

Hope this will be helpful for someone. Thanks for all the comments and hints to search for the solution.

Upvotes: 3

Related Questions