我可以在 EF.Net
中做到这一点,但在 EFCore
中不行
List<string> keyList = string.IsNullOrEmpty(keywords) ? new List<string>() : keywords.Split(' ').ToList();
collections = await db.ProductCollections
.Where(m => m.Children.Count == 0 && (!keyList.Any() ? true : keyList.All(x => m.Name.Contains(x))))
.ToListAsync();
我改成:
collections = await db.ProductCollections
.Where(m => m.Children.Count == 0 && (keyList.Count == 0 || keyList.All(x => m.Name.Contains(x))))
.ToListAsync();
所以我猜问题出在keyList.All()
。如何在 EFCore 中实现这一目标?
错误信息:
<块引用>LINQ 表达式 'DbSet()
.Where(p => DbSet()
.Where(p0 => EF.Property
答案 0 :(得分:1)
在执行之前基于键列表构建查询的替代方法。
var keys = string.IsNullOrEmpty(keywords) ? Array.Empty<string>() : keywords.Split(' ');
var query = db.ProductCollections.Where(p => p.Children.Any() == false);
foreach (var key in keys)
{
query = query.Where(p => p.Name.Contains(key));
}
var collections = await query.ToListAsync();