EfCore ToListAsync() 在 where 条件下抛出异常

EfCore ToListAsync() Throw exception with where condition

我在使用特定条件从数据库中获取项目的异步任务时遇到问题 当我调用 ToListAsync() 时出现此错误

Expression of type 'System.Collections.Generic.IAsyncEnumerable1[System.Guid]' cannot be used for constructor parameter of type 'System.Collections.Generic.IEnumerable1[System.Guid]' Parameter name: arguments[0]

这是代码片段:

public async Task<IEnumerable<Parent>> GetItems(List<Guid> TypeIds)
{
    var items = context.Parent.Include(x => x.Child).Where(x => new HashSet<Guid>(x.Child.Select(y => y.TypeId).Distinct()).SetEquals(new HashSet<Guid>(TypeIds)));
    return await items.Include(x=> x.Child).ToListAsync();
}

如果我不实施此方法 async 我将不会收到错误并且一切正常。

您不能像 Entity Framework 那样使用 Where lambda,请记住表达式将被转换为 SQL,它不知道 [=12] 是什么=] 是。您可能正在寻找这样的东西:

var items = context.Parent
    .Include(x => x.Child)
    .Where(x => x.Child.Any(y => TypeIds.Contains(y.TypeId)));