Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态设置LINQ数据源会导致运算符'==';与操作数类型不兼容';字符串';和';Int32';_C#_Asp.net_Linq - Fatal编程技术网

C# 动态设置LINQ数据源会导致运算符'==';与操作数类型不兼容';字符串';和';Int32';

C# 动态设置LINQ数据源会导致运算符'==';与操作数类型不兼容';字符串';和';Int32';,c#,asp.net,linq,C#,Asp.net,Linq,我正在尝试为linq数据源动态设置where语句,在我尝试添加另一个约束之前,它工作得很好 此代码在此处工作: private void SetDataSourceWhereStatement() { HttpCookie cookie = Request.Cookies[ "CustomerID" ]; if ( cookie == null ) //set data

我正在尝试为linq数据源动态设置where语句,在我尝试添加另一个约束之前,它工作得很好

此代码在此处工作:

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 + " && CategoryID == " + m_CategoryID;
        ldsCustomerLinks.DataBind();
    }
然而,当我尝试添加一个客户号码时,我得到了错误。这是我尝试使用的代码:

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();
    }
cookie和cookie2值都是字符串。我尝试过使用int.parse、int32.parse转换它,使用value的.toString()方法,并尝试了所有方法。我不明白怎么了


编辑::

所以我从下面的一个帖子中得到了答案,但我不明白,有人能解释一下为什么我原来的代码不起作用,而修改后的代码起作用吗

旧代码:

if ( cookie2 != null )
            ldsCustomerLinks.Where += " && CustomerNumber == " + cookie2.Value;
新代码:

if ( cookie2 != null )
            ldsCustomerLinks.Where += @" && CustomerNumber == (""" + cookie2.Value + @""") ";

你说
cookie
cookie2
是字符串。。。那么m_CategoryID呢

你试过了吗

ldsCustomerLinks.Where = ldsCustomerLinks.Where + " && CategoryID == " + m_CategoryID.ToString();

好的-我怀疑CustomerNumber在“Where”字符串中缺少引号,但没有足够的信心将其作为建议发布(糟糕的一天!)

因此,要回答第二个问题:

类型不匹配出现在数据库中,其中CustomerNumber(我推断)是一个字符串,但您正在使用一个数字构建一个查询,如

CustomerID==312&&CustomerNumber==45654789&&CategoryID==3

你应该说什么

CustomerID==312&&CustomerNumber==45654789&&CategoryID==3


要获取字符串中的引号(用引号括起来),您需要使用“”语法。

请原谅,如果不太了解LINQ的用法,我的答案可能会完全错误


但似乎相关,并建议您分析/转换(在这种情况下引用!)值。

最终的Where属性值是什么样子的?重新标记asp.net-如果这是错误的,请告诉我。
的结果值是什么。
如果在
数据绑定()上设置断点,Where
?@lass:“CustomerID==312&&CustomerNumber==45654789&&CategoryID==3“不,那以前工作正常。它将做的是为该类别的客户提供服务,并且它按照它应该做的那样工作。现在我试图进一步缩小范围,当用户从ddl中选择一个客户号码时,它应该只显示类别中的号码。哦,m_CategoryID是一个intAh。非常感谢。那么为什么要包含“@”符号呢?@意味着“准确地使用以下文本,而不转义”,对于指定诸如文件路径之类的内容特别有用-例如,@“C:\Folder1”而不是“C:\\Folder1”:)不,使用int以这种方式强制转换它。Parse会产生“类型“CustomerLink”中不存在属性或字段“int”。这和他上一篇我没看到的帖子是一样的。有人能解释为什么这条线能用,而我以前的那条不行吗?如果(cookie2!=null)ldsCustomerLinks.Where+=@“&&CustomerNumber==(“+cookie2.Value+@”);将此问题包含在我的OP中以便于查看。