Like parameters must be of string value!

A

Andy B.

I ran across this entity framework error: 'Like parameters must be of string
value.' Here is the line of code it mentioned:

Return DBContext.NewsArticles.Where("year(it.CreationDate) like @Year and
it.Archived = true", New ObjectParameter("Year",
Year)).OrderBy("it.CreationDate desc")



The Year parameter is a string since the function parameter was a string and
it got the value from a DropDownList.SelectedItem.Value which is also a
string. What is my problem and how would I fix it?
 
M

miher

Hi,

Im not sure if the problem is this, but as i know 'and' has higher
precedence than 'like'. (at least in SQL)
-Zsolt
 
A

Andy B.

Didn't work. I think and has no priority since it is basically sql version
of addition or string addition. Any other ideas?
 
P

Paul Shapiro

That's correct. Year(date) is an integer. If your comparison needs to be in
SQL Server syntax, you should use the integer comparison for the year and a
numeric comparison for Archived, which I assume is boolean:
Where("year(it.CreationDate) = Cast(@Year as int) and it.Archived <> 0")

If the comparison is being done in your code, then replace the Cast() with a
suitable function.
 
A

Andy B.

Is there any way to fix this withought having to go all the way through sql
server to create a stored procedure just to do a simple search? I need to
return all records if the % is given as a value (last I knew this was an sql
server wildcard). Otherwise I just need to return the results based on the
year number.
 
A

Andy B.

I tried just about everything for this part of the where statement:
Cast(year(CreationDate) as string) like @Year

I keep getting errors like int, string, Date, DateTime, varchar and whatever
else datatype you can conjure up isn't valid in the current schema. I am
stuck because entity framework appears to require both sides of a like
statement to be string. This poses a problem because I am sending years to
the query and one of the values to the years is a %. I have to be able to
return all of the years if % is choosen as well as returning based on the
year itself.
 
H

Hans Kesting

Andy B. laid this down on his screen :
Is there any way to fix this withought having to go all the way through sql
server to create a stored procedure just to do a simple search? I need to
return all records if the % is given as a value (last I knew this was an sql
server wildcard). Otherwise I just need to return the results based on the
year number.

% is a wildcard for SQL, but only for string searches (with LIKE), not
for integer values (where LIKE is not supported).

If you have either a % or a year, you might be able to use something
like:

@year = '%' OR Year(theDate) = convert(int, @year)


Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top