实体框架核心在哪里

时间:2021-01-04 04:42:07

标签: c# entity-framework-core

我可以在 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(p, "ID") != null && object.Equals( objA: (object)EF.Property(p, "ID"), objB: (object)EF.Property(p0, "ParentId"))) .Count() == 0 && False || __keyList_0 .All(x => p.Name.Contains(x)))' 无法翻译。要么以可翻译的形式重写查询,要么切换 通过插入对“AsEnumerable”的调用来显式地进行客户端评估, “AsAsyncEnumerable”、“ToList”或“ToListAsync”。看 https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。

1 个答案:

答案 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();