简体   繁体   English

实体框架核心

[英]Entity Framework Core Where All

I can do this fine in EF.Net , but not in EFCore我可以在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();

I changed it into:我把它改成:

collections = await db.ProductCollections
    .Where(m => m.Children.Count == 0 && (keyList.Count == 0 || keyList.All(x => m.Name.Contains(x))))
    .ToListAsync();

So I guess the problem is in keyList.All() .所以我想问题出在keyList.All()中。 How can I achieve this in EFCore?如何在 EFCore 中实现这一点?

The error message:错误信息:

The LINQ expression 'DbSet().Where(p => DbSet().Where(p0 => EF.Property<Nullable>(p, "ID").= null && object:Equals( objA. (object)EF,Property<Nullable>(p, "ID"): objB. (object)EF,Property<Nullable>(p0. "ParentId"))).Count() == 0 && False || __keyList_0.All(x => p.Name.Contains(x)))' could not be translated, Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList'. The LINQ expression 'DbSet().Where(p => DbSet().Where(p0 => EF.Property<Nullable>(p, "ID").= null && object:Equals( objA. (object)EF, Property<Nullable>(p, "ID"): objB.(object)EF,Property<Nullable>(p0."ParentId"))).Count() == 0 && False || __keyList_0.All(x => p.Name.Contains(x)))' 无法翻译,要么以可翻译的形式重写查询,要么通过插入对 'AsEnumerable'、'AsAsyncEnumerable'、'ToList' 的调用显式切换到客户端评估. or 'ToListAsync'.或“ToListAsync”。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

Alternative approach to build query based on key list, before executing it.在执行之前基于键列表构建查询的替代方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM