1

最近我开始在我们的项目中使用EF+,并取得了巨大的成功,但有时我会遇到一个问题,即我有一组实体需要运行单独的查询。

因此,如果我有一组 20 个客户,我需要分别运行 20 个查询。我想知道是否有办法在 foreach 循环中以某种方式使用 EF+ FutureValue() 来避免这种情况。

请参阅此示例代码:

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?
                }
4

1 回答 1

2

您需要首先生成所有“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;
}
于 2018-11-16T14:13:37.090 回答