2014-05-05 129 views
1

我試圖寫一個搜索程序和它的一部分包括下面的代碼:LINQ Where子句行爲

string searchValue = Server.HtmlEncode(RadSearchBox.Text.ToLower()); 

var injurySearchList = (from i in injuryObj 
         where i.IsDeleted == false && 
         (
          i.ResultOther == true ? "ResultOther".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue) 
          || i.BodyPartHead == true ? "BodyPartHead".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue) 
         ) 
         select i.IncidentID).ToList(); 

由於我無法搜索到比特值的字符串,我用的是三元運營商評估是否屬實。如果是這樣,我[手動]將字段名稱轉換爲字符串,看看是否包含搜索字符串。如果位值爲假,我寧願從WHERE子句中排除該行,但無法提出任何聰明的想法,所以我使用IncidentID字段轉換爲帶有Contains()方法的字符串作爲填料。

使用下面的數據集爲例:

+------------+-------------+--------------+-----------+ 
| IncidentID | ResultOther | BodyPartHead | IsDeleted | 
+------------+-------------+--------------+-----------+ 
|   1 |   0 |   1 |   0 | 
|   2 |   1 |   1 |   0 | 
|   3 |   0 |   1 |   0 | 
+------------+-------------+--------------+-----------+ 

的問題是,當searchValue等於「體」,它只是返回IncidentID 1和3,但2應該在那裏。相反,如果searchValue等於「結果」,它只會按照預期返回IncidentID 2。

任何想法?

回答

1

您的訂單有問題。 (||)在之前被評估爲條件三元運算符。你需要圍繞這兩個OR操作用括號:

where i.IsDeleted == false && 
(
    (i.ResultOther == true ? "ResultOther".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue)) 
    || (i.BodyPartHead == true ? "BodyPartHead".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue)) 
) 

你原來的代碼就相當於:

where i.IsDeleted == false && 
(
    i.ResultOther == true 
     ? "ResultOther".ToLower().Contains(searchValue) 
     : (i.IncidentID.ToString().Contains(searchValue)) || (i.BodyPartHead == true) 
       ? "BodyPartHead".ToLower().Contains(searchValue) 
       : i.IncidentID.ToString().Contains(searchValue)) 
) 

(你也可以擺脫== true和使用!代替== false,但那些別沒有改變的結果,只是表面上的變化)

+0

正確的答案,也是有用的,謝謝。 –

+0

不客氣。順便說一句,你接受的答案不是第一(我不在乎你接受的只是確保你使用正確的標準)。 –

+0

我剛纔說過每個名字上面都寫着「回答##分鐘前」。我在哪裏可以找到日期/時間戳? –

1

我相信你需要parantesis圍繞內部? :陳述,如下所示:

var injurySearchList = (from i in injuryObj 
         where i.IsDeleted == false && 
         (
          (i.ResultOther == true 
           ? "ResultOther".ToLower().Contains(searchValue) 
           : i.IncidentID.ToString().Contains(searchValue)) 
         || (i.BodyPartHead == true 
           ? "BodyPartHead".ToLower().Contains(searchValue) 
           : i.IncidentID.ToString().Contains(searchValue)) 
          ) 
         select i.IncidentID).ToList(); 

從你如何描述你真正想要實現的目標,我認爲以下簡化爲您提供了相同的結果:

var injurySearchList2 = (from i in injuryObj 
         where !i.IsDeleted 
         && (
          (i.ResultOther && "resultother".Contains(searchValue)) 
          || (i.BodyPartHead && "bodyparthead".Contains(searchValue)) 
         ) 
         select i.IncidentID).ToList(); 
+0

正確的答案,並感謝您的簡化,這更好的作品。我會投這個票,但還沒有代表。 –

1

你需要把周圍的兩個條件括號。第二個是越來越裹成第一,像這樣:

where i.IsDeleted == false && 
(
    i.ResultOther == true ? "ResultOther".ToLower().Contains(searchValue) : (i.IncidentID.ToString().Contains(searchValue) || i.BodyPartHead == true ? "BodyPartHead".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue)) 
) 

應該是:

where i.IsDeleted == false && 
(
    (i.ResultOther == true ? "ResultOther".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue)) 
    || (i.BodyPartHead == true ? "BodyPartHead".ToLower().Contains(searchValue) : i.IncidentID.ToString().Contains(searchValue)) 
) 
+0

就是這樣。其他答案很好,但你是第一個。謝謝! –