2013-08-29 96 views
0

我得到這個工作確定:當我使用使用||的Linq搜索查詢

var results = db.GetProducts().Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase)); 

||

var results = db.GetProducts().Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase)) || db.GetProducts().Where(p => p.ProductId.ToString().Equals(searchQuery, StringComparison.OrdinalIgnoreCase)); 

我結束了:作爲對比,讓我的新的查詢

Error 1 Operator '||' cannot be applied to operands of type 
'System.Collections.Generic.IEnumerable<Uppgift_1.Models.MyProduct>' and 
'System.Collections.Generic.IEnumerable<Uppgift_1.Models.MyProduct>' C:\Home\Programming 
\cSharp\projects\MVC_projects\Uppgift_1\Uppgift_1\Controllers\ProductController.cs 
    23 27 Uppgift_1 

我該怎麼辦,如果我想包括在搜索查詢兩個語句?

+0

只需讀取錯誤文本。你應用||到兩個序列。 – Vladimir

回答

1

您需要將兩個條件放在同一個Where

var results = db.GetProducts() 
       .Where(p => p.ProductName 
          .Equals(searchQuery, 
            StringComparison.OrdinalIgnoreCase) || 
          p.ProductId.ToString() 
          .Equals(searchQuery, 
            StringComparison.OrdinalIgnoreCase)); 
+0

哦,我明白了,謝謝! –

1

試試這個:

var results = db.GetProducts() 
    .Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase) 
    || p.ProductId.ToString().Equals(searchQuery, StringComparison.OrdinalIgnoreCase));