【发布时间】:2021-05-01 19:52:33
【问题描述】:
我正在摸索如何实现这一目标
我有一张产品和变体表。
假设我的产品表中有一条名为 Sony Monitor
的记录在我的变体表中,我有一个相关变体,其 VariationName 为 32"
如果我按如下方式构造我的 linq:
var productSearch = products.Where(p => p.Name.Contains(searchTerm) || p.Variation.Name.Contains(searchTerm)
“Sony” 会产生搜索结果。 32" 会产生一个搜索结果。
Sony 32" 不会产生搜索结果。
实现这一目标的最佳方法是什么?
编辑
为了便于使用,我为我的搜索结果创建了一个 ViewModel (ProductSearch)。我将“匹配”添加为整数字段。
我合并我的产品和变体表以获取结果列表。我遇到问题的代码如下:
string searchTerm = "Sony 32";
string[] searchTerms = searchTerm.Split(' ');
//Concat my two queries (this works fine without the new search code)
var productSearch = rootProducts.Concat(variableProducts)
.OrderBy(p => p.Name)
.Select(p => new ProductSearch()
{
InternalSku = (string)p.InternalSku,
ProductId = (int)p.ProductId,
ProductVariationId = (int?)p.ProductVariationId,
Name = (string)p.Name,
ManufacturerSku = (string)p.ManufacturerSku,
Ean = (string)p.Ean,
ImageUrl = p.ImageUrl,
WebDisplay = (bool)p.WebDisplay,
VariationName = (string)(p.Name),
Matches =
new[] { p.Name, p.VariationName }
.SelectMany(x => searchTerms, (x, y) => x.Contains(y))
.Count(),
})
.Skip(skip)
.Take(take)
.ToList();
我收到的错误是:
无法翻译 LINQ 表达式“x”。以可翻译的形式重写查询,或通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端评估。
【问题讨论】:
-
你能指定哪个LINQ:LINQ to SQL / EF 6.x / EF Core 2.0 / 2.1 / 3.x / 5.x?
-
是的,我使用的是 EF Core 5.0.1