2010-01-04 67 views
2

我試圖動態地設置我的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 + @""") "; 
+0

最終的Where屬性值是什麼樣的? – 2010-01-04 19:26:05

+0

重新標記的asp.net - 讓我知道如果這是錯誤的。 – 2010-01-04 19:27:01

+0

如果你在'DataBind()'上設置了一個breakpoin,'.Where'的結果值是多少? – 2010-01-04 19:32:29

回答

1

原諒我,沒有太多關於這個LINQ用法的知識,我的答案可能是完全關閉的。

This question似乎是相關的,並建議你解析/轉換(引用在這種情況下!)值。

+0

不能,使用int.Parse以這種方式進行轉換會產生「在'CustomerLink'類型中不存在屬性或字段'int'」 – Justen 2010-01-04 19:47:11

+0

Er,nvm。它確實與他沒有看到的最後一篇文章一起工作。有人可以解釋爲什麼這條線在我的舊線沒有使用時起作用嗎? if(cookie2!= null) ldsCustomerLinks.Where + = @「&& CustomerNumber ==(」「」+ cookie2.Value + @「」「)」; – Justen 2010-01-04 19:50:50

+0

將此問題包含在我的OP中以方便查看。 – Justen 2010-01-04 19:54:07

1

你說cookiecookie2都是字符串...怎麼樣m_CategoryID

你試過

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

行 - 我懷疑在「去哪兒」的字符串缺少引號爲CUSTOMERNUMBER,但沒有足夠的信心將它張貼的建議(有一個糟糕的!就這樣一天)

所以要回答第二個問題:

類型不匹配而來的數據庫,其中CUSTOMERNUMBER(我推斷)是一個字符串內,但你buildi NG了一些查詢,像

客戶id == 312 & & CUSTOMERNUMBER == 45654789 & &類別ID == 3

,你應該說

客戶id == 312 & & CUSTOMERNUMBER == 「45654789」& & CategoryID == 3

並且得到一個字符串中的引號(用引號括起來)你需要使用「」「語法。

+0

不,之前工作正常。它會做的是提出客戶,爲這個類別,它的工作原理應該如此。現在我試圖通過用戶從ddl中選擇一個客戶號碼進一步縮小範圍,我應該只顯示來自類別的那個號碼。 – Justen 2010-01-04 19:31:51

+0

哦,並且m_CategoryID是int – Justen 2010-01-04 19:33:57

+0

啊我看到了。謝謝。那麼爲什麼包含'@'符號呢? – Justen 2010-01-04 21:34:10

相關問題