我試圖動態地設置我的LINQ數據源的where語句,它工作正常,直到我嘗試添加另一個約束。動態設置運算符'=='的LINQ數據源結果與操作數類型'字符串'和'Int32'不兼容
此代碼的工作在這裏:
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,使用值的.toString()方法轉換它,並嘗試一切。我不明白什麼是錯的。
編輯::
所以我得到了我的答案,從下面的職位之一,但我不明白,可能有人請解釋爲什麼我原來的代碼沒有工作,但修改後的呢?
舊代碼:
if (cookie2 != null)
ldsCustomerLinks.Where += " && CustomerNumber == " + cookie2.Value;
新代碼:
if (cookie2 != null)
ldsCustomerLinks.Where += @" && CustomerNumber == (""" + cookie2.Value + @""") ";
最終的Where屬性值是什麼樣的? – 2010-01-04 19:26:05
重新標記的asp.net - 讓我知道如果這是錯誤的。 – 2010-01-04 19:27:01
如果你在'DataBind()'上設置了一個breakpoin,'.Where'的結果值是多少? – 2010-01-04 19:32:29