我让这个工作正常:
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 :(得分:1)
您需要将两个条件放在同一个Where
。
var results = db.GetProducts()
.Where(p => p.ProductName
.Equals(searchQuery,
StringComparison.OrdinalIgnoreCase) ||
p.ProductId.ToString()
.Equals(searchQuery,
StringComparison.OrdinalIgnoreCase));
答案 1 :(得分:1)
试试这个:
var results = db.GetProducts()
.Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase)
|| p.ProductId.ToString().Equals(searchQuery, StringComparison.OrdinalIgnoreCase));