gpt4 book ai didi

entity-framework-core - 表达式中的EF Core 3 x.Contains(),其中x是ICollection

转载 作者:行者123 更新时间:2023-12-04 16:57:24 30 4
gpt4 key购买 nike

我有以下数据层设置:

public class Repository : IRepository {

private readonly MyDbContext _dbContext;

public List<Meter> Search(Expression<Func<Meter,bool>> criteria)
IQueryable<Meter> results = _dbContext.Meters;
return results.Where(criteria).ToList();
}
}
}

... from a client class:

IRepository _repository;

public void ClientMethod () {

ICollection<int> ids = new List<int>() {1, 2, 3);
var results = _repository.Search(c=> ids.Contains(c.Id)); // This throws exception

}

这将导致以下异常:

expression Where(source: DbSet, predicate: (m) => (Unhandled parameter: __ids_0).Contains(m.Id))' 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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()



但是,如果我将集合引用更改为IEnumerable或List,它将起作用:
public void ClientMethod () {

// This works
List<int> ids = new List<int>() {1, 2, 3);
var results = _repository.Search(c=> ids.Contains(c.Id));

// This works
IEnumerable<int> ids = new List<int>() {1, 2, 3);
var results = _repository.Search(c=> ids.Contains(c.Id));
}

为什么对ICollection不起作用,对IEnumerable和List不起作用?我的许多客户端方法都将ICollection作为参数。

我使用的是EF Core 3.0,但我认为我在2.1中也遇到了同样的问题,它没有抛出错误,而是在客户端上对其进行了评估。

最佳答案

这是3.1中修复的已知错误。由于查询管道重写后的所有回归,3.0几乎不可用。

我建议在github上关注ef core的问题跟踪器。

关于entity-framework-core - 表达式中的EF Core 3 x.Contains(),其中x是ICollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58156883/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com