Dynamically setting LINQ datasource results in Operator '==' incompatible with operand types 'String' and 'Int32'
I'm trying to dynamically set my where statements for a linq datasource, and it was working fine until I try to add another constraint.
This code works here:
private void SetDataSourceWhereStatement()
{
HttpCookie cookie = Request.Cookies[ "CustomerID" ];
if ( cookie == null ) //set data source where statement to default
ldsCustomerLinks.Where = "CustomerID == -1";
else ldsCustomerLinks.Where = "CustomerID == " + cookie.Value; //set data source where statement
ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && Cat开发者_JAVA技巧egoryID == " + m_CategoryID;
ldsCustomerLinks.DataBind();
}
However, when I also try and add a customer number, I get the error. This is the code I'm attempting to use:
private void SetDataSourceWhereStatement()
{
HttpCookie cookie = Request.Cookies[ "CustomerID" ];
HttpCookie cookie2 = Request.Cookies[ "CustomerNumber" ];
if ( cookie == null ) //set data source where statement to default
ldsCustomerLinks.Where = "CustomerID == -1";
else ldsCustomerLinks.Where = "CustomerID == " + cookie.Value; //set data source where statement
if ( cookie2 != null )
ldsCustomerLinks.Where += " && CustomerNumber == " + cookie2.Value;
// else ldsCustomerLinks.Where += " && CustomerNumber >= 0";
ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && CategoryID == " + m_CategoryID;
ldsCustomerLinks.DataBind();
}
Both cookie and cookie2 values are strings. I've tried converting it using int.parse, int32.parse, using the value's .toString() methods and trying everything all over. I don't understand what's wrong.
EDIT::
So I got my answer, from one of the below posts, but I do not understand, could someone please explain why my original code did not work but the revised does?
Old code:
if ( cookie2 != null )
ldsCustomerLinks.Where += " && CustomerNumber == " + cookie2.Value;
New code:
if ( cookie2 != null )
ldsCustomerLinks.Where += @" && CustomerNumber == (""" + cookie2.Value + @""") ";
Forgive me, without much knowledge about this LINQ usage my answer might be totally off.
This question seems to be related though and suggests that you parse/convert the (quoted in that case!) value.
You say cookie
and cookie2
are strings... what about m_CategoryID
?
have you tried
ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && CategoryID == " + m_CategoryID.ToString();
OK - I was suspicious of the lack of quotes for CustomerNumber in the 'Where' string, but not confident enough to post it as a suggestion (having a bad SO day!)
So to answer the second question:
the type mismatch comes within the database, where CustomerNumber (I am inferring) is a string, but you are building a query with a number, like
CustomerID == 312 && CustomerNumber == 45654789 && CategoryID == 3
where you should be saying
CustomerID == 312 && CustomerNumber == "45654789" && CategoryID == 3
and to get the quotes within a string (which is enclosed by quotes) you need to use the """ syntax.
精彩评论