This is a migrated thread and some comments may be shown as answers.

linq with Contains issue

3 Answers 68 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Hans
Top achievements
Rank 1
Hans asked on 18 Mar 2011, 10:14 PM
Hello !

I have this code
 
IList  <TwoFieldsClass> tempList1 = (from c in _localContext.CustomerMasters where c.CUSTNMBR.Contains(searchCriteria) select new TwoFieldsClass 
{ Field1 = c.CUSTNMBR, Field2 = c.CUSTNAME }).OrderBy(f => f.Field1).ToList();

 

for searchcriteria = "p"

it generate the sql

 

 

 

declare @p1 int
set @p1=1
exec sp_prepexec @p1 output,N'@p0 nchar(65)',N'SELECT a.[CUSTNMBR] AS COL1, a.[CUSTNAME] AS COL2 FROM [RM00101] a WHERE a.[CUSTNAME] LIKE ''%'' + @p0 + ''%'' ESCAPE ''\'' ',@p0=N'p'
select @p1

 


but I have no results. templist1 has zero records
if I query direct in sql server

select custnmbr,custname from rm000101 where custname like '%p%'
i have a bunch of records

So my question is why the linq doesn't return anything ?

Regards,
Hans

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 21 Mar 2011, 03:47 PM
Hi Hans,

I believe this could come from a char(x) or nchar(x) column; is CUSTNMBR mapped to such a column type? I have just delivered a change that will make avoid this issue for such column types. To work around this issue, please use  c.CUSTNMBR.Contains(searchCriteria+"%").

Greetings,
Thomas
the Telerik team
0
Hans
Top achievements
Rank 1
answered on 21 Mar 2011, 04:15 PM
Hi !

Thanks for your answer.
The field is nvarchar
I found another work around
IList<TwoFieldsClass> tempList1 = (from c in _localContext.CustomerMasters where c.CUSTNMBR.Trim().Contains(searchCriteria)  select new TwoFieldsClass { Field1 = c.CUSTNMBR, Field2 = c.CUSTNAME }).OrderBy(f => f.Field1).ToList();

So If i call trim befor contains, generate correct the sql.

You said you just delivered a version thar fixed this error ? Where ? I just installed 2011 Q1.

Hans
0
Thomas
Telerik team
answered on 23 Mar 2011, 08:58 PM
Hello Hans,

ok, this is a valid work around. The translated Trim() will cause the non-column type converter to be used which will pass the string correctly to the server. 
As stated already, I've fixed the converter and the next patch will contain the changes so that Trim() would no longer be required.

Greetings,
Thomas
the Telerik team
Tags
LINQ (LINQ specific questions)
Asked by
Hans
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Hans
Top achievements
Rank 1
Share this question
or