如何解决实体框架增强-foreach循环中的Future
我的问题与Entity Framework Plus - FutureValue() in a foreach loop非常相似。
public class CustomerService
{
private readonly EntityContext Context = new EntityContext();
private IQueryable<Customer> MakeQuery(object query)
{
return this.Context.Customers
// Omitted more logic here to create a complex query.
.AsQueryable();
}
public List<List<Customer>> GetCustomersBulk(List<object> queries)
{
var response = new List<List<Customer>>();
var futureQueries = new List<QueryFutureEnumerable<Customer>>();
foreach (var query in queries)
{
futureQueries.Add(
this.MakeQuery(query: query)
.Future()
);
}
foreach (var futureQuery in futureQueries)
{
response.Add(futureQuery.ToList());
}
return response;
}
}
完全工作的小提琴:https://dotnetfiddle.net/vg8pjA
但是,使用Futures
似乎比不使用它们要慢。
如果我尝试使用相关问题中的the answer,就像使用FutureValue
一样,我的查询将仅返回1个Customer,而不返回其中的许多。我认为使用该代码,查询可以在一次数据库旅行中运行,但是结果不是我想要的。
我尝试使用.FutureValue<IEnumerable<Customer>>()
,但随后出现此错误:
Error CS1929 'IQueryable<Customer>' does not contain a deFinition for 'FutureValue' and the best extension method overload 'QueryFutureExtensions.FutureValue<IEnumerable<Customer>>(IQueryable<IEnumerable<Customer>>)' requires a receiver of type 'IQueryable<IEnumerable<Customer>>'
请注意,我正在使用Z.EntityFramework.Plus.EFCore
版的2.0.60
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。