0

我让它工作正常:

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

如果我想在搜索查询中包含这两个语句,我该怎么办?

4

2 回答 2

1

您需要将两个条件放在同一个Where.

var results = db.GetProducts()
                .Where(p => p.ProductName
                             .Equals(searchQuery, 
                                     StringComparison.OrdinalIgnoreCase) ||
                            p.ProductId.ToString()
                             .Equals(searchQuery,
                                     StringComparison.OrdinalIgnoreCase));
于 2013-08-29T10:19:20.380 回答
1

尝试这个:

var results = db.GetProducts()
    .Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase)
    || p.ProductId.ToString().Equals(searchQuery, StringComparison.OrdinalIgnoreCase));
于 2013-08-29T10:19:28.350 回答