Entity Framework Plus-foreach循环中的FutureValue() [英] Entity Framework Plus - FutureValue() in a foreach loop

查看:44
本文介绍了Entity Framework Plus-foreach循环中的FutureValue()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始在我们的项目中成功使用 EF + ,但是有时候我遇到了一个问题有一组实体,我需要为其运行单独的查询.

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.

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

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.

请参阅以下示例代码:

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"查询,然后才能使用它们.

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

这篇关于Entity Framework Plus-foreach循环中的FutureValue()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆