Entity framework foreach循环中的Entity Framework Plus-FutureValue()
最近,我开始在我们的项目中使用,并取得了巨大的成功,但有时我会遇到一个问题,即我有一组实体,需要对其运行单独的查询 因此,如果我有一组20个客户,我需要分别运行20个查询。我想知道是否有办法在foreach循环中使用EF+FutureValue()来避免这种情况 请参见此示例代码:Entity framework foreach循环中的Entity Framework Plus-FutureValue(),entity-framework,entity-framework-6,entity-framework-plus,Entity Framework,Entity Framework 6,Entity Framework Plus,最近,我开始在我们的项目中使用,并取得了巨大的成功,但有时我会遇到一个问题,即我有一组实体,需要对其运行单独的查询 因此,如果我有一组20个客户,我需要分别运行20个查询。我想知道是否有办法在foreach循环中使用EF+FutureValue()来避免这种情况 请参见此示例代码: foreach (var customer in customers) { customer.SomeValue = ctx.SomeDatab
foreach (var customer in customers)
{
customer.SomeValue = ctx.SomeDatabaseTable.Where(myCondition).FutureValue();
// this would run 20 times ... any way how to run all of the 20 queries at once?
}
首先需要生成所有“QueryFuture”查询,然后才能使用它们 所以两个循环应该可以让它工作
var futureQueries = new List<BaseQueryFuture>();
// create all query futures
for(int i = 0; i < customers.Length; i++)
{
futureQueries.Add(ctx.SomeDatabaseTable.Where(myCondition).FutureValue());
}
// assign result (the first solved will make the call to the DB)
for(int i = 0; i < customers.Length; i++)
{
customer.SomeValue = ((QueryFutureValue<SomeValueType>)futureQueries[i]).Value;
}
var futureQueries=new List();
//创建所有查询未来
for(int i=0;i