Entity Framework Plus - FutureValue() in a foreach loop

怎甘沉沦 提交于 2021-02-08 07:33:27

问题


Recently I started using EF+ on our project with great success however sometimes I run into an issue that I have a set of entities for which I need to run a separate query.

So if I have a set of 20 customers I need to run 20 queries separately. I wonder if there is way how to avoid this using EF+ FutureValue() somehow in a foreach loop.

See this sample code:

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

回答1:


You need first to generate all "QueryFuture" queries then you can use them.

So two loop should make it works.

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;
}


来源:https://stackoverflow.com/questions/53334642/entity-framework-plus-futurevalue-in-a-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!