我可以使用实体框架在单个数据库调用中轻松评估多个 IQueryable 吗?

发布于 2024-12-27 09:14:17 字数 1136 浏览 2 评论 0原文

假设我有一个 IQueryable 的集合(任意大小)(全部针对相同的 MyEntity 类型)。每个单独的查询都已成功动态构建,以将各种业务逻辑封装成可以在单个数据库行程中进行评估的形式。现在有什么方法可以让所有这些IQueryable在到数据库的单次往返中执行吗?

例如(简化;我的实际查询更复杂!),如果我希望

ObjectContext context = ...;
var myQueries = new[] {
    context.Widgets.Where(w => w.Price > 500),
    context.Widgets.Where(w => w.Colour == 5),
    context.Widgets.Where(w => w.Supplier.Name.StartsWith("Foo"))
};

EF 执行每个查询的翻译(它可以单独执行),那么在一次数据库访问中,执行

SELECT * FROM Widget WHERE Price > 500
SELECT * FROM Widget WHERE Colour = 5
SELECT W.* FROM Widget 
                INNER JOIN SUpplier ON Widget.SupplierId = Supplier.Id 
           WHERE Supplier.Name LIKE 'Foo%'

然后将每个结果集转换为 IEnumerable,以通常的方式更新 ObjectContext

我看过关于处理存储过程中的多个结果集的各种帖子,但这略有不同(尤其是因为我在编译时不知道会有多少个结果集) )。有没有一种简单的方法,或者我必须使用类似 的方法实体框架是否支持使用单个存储过程返回多个结果集的能力?

Suppose I have a collection (of arbitrary size) of IQueryable<MyEntity> (all for the same MyEntity type). Each individual query has successfully been dynamically built to encapsulate various pieces of business logic into a form that can be evaluated in a single database trip. Is there any way I can now have all these IQueryables executed in a single round-trip to the database?

For example (simplified; my actual queries are more complex!), if I had

ObjectContext context = ...;
var myQueries = new[] {
    context.Widgets.Where(w => w.Price > 500),
    context.Widgets.Where(w => w.Colour == 5),
    context.Widgets.Where(w => w.Supplier.Name.StartsWith("Foo"))
};

I would like to have EF perform the translation of each query (which it can do indivudually), then in one database visit, execute

SELECT * FROM Widget WHERE Price > 500
SELECT * FROM Widget WHERE Colour = 5
SELECT W.* FROM Widget 
                INNER JOIN SUpplier ON Widget.SupplierId = Supplier.Id 
           WHERE Supplier.Name LIKE 'Foo%'

then convert each result set into an IEnumerable<Widget>, updating the ObjectContext in the usual way.

I've seen various posts about dealing with multiple result sets from a stored procedure, but this is slightly different (not least because I don't know at compile time how many results sets there are going to be). Is there an easy way, or do I have to use something along the lines of Does the Entity Framework support the ability to have a single stored procedure that returns multiple result sets??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2025-01-03 09:14:17

不可以。EF deos 没有查询批处理(未来查询)。一次可查询就是一次数据库往返。作为解决方法,您可以尝试使用它,例如使用:

string sql = ((ObjectQuery<Widget>)context.Widgets.Where(...)).ToTraceString();

获取查询的 SQL 并从要执行的所有 SQL 构建您自己的自定义命令。之后,您可以使用与存储过程类似的方法来转换结果。

除非您确实需要单独执行每个查询,否则您也可以将它们合并为单个查询:

context.Widgets.Where(...).Union(context.Widgets.Where(...));

这将产生 UNION。如果您只需要 UNION ALL 您可以使用 Concat 方法。

No. EF deosn't have query batching (future queries). One queryable is one database roundtrip. As a workaround you can try to play with it and for example use:

string sql = ((ObjectQuery<Widget>)context.Widgets.Where(...)).ToTraceString();

to get SQL of the query and build your own custom command from all SQLs to be executed. After that you can use similar approach as with stored procedures to translate results.

Unless you really need to have each query executed separately you can also union them to single query:

context.Widgets.Where(...).Union(context.Widgets.Where(...));

This will result in UNION. If you need just UNION ALL you can use Concat method instead.

淡淡绿茶香 2025-01-03 09:14:17

可能回复晚了,希望它能帮助其他遇到同样问题的人。

NuGet 上有实体框架扩展库,它提供了未来查询功能(等等)。我玩了一下,看起来很有希望。

您可以在此处找到更多信息。

It might be late answer, hopefully it would help some one else with the same issue.

There is Entity Framework Extended Library on NuGet which provides the future queries feature (among others). I played a bit with it and it looks promising.

You can find more information here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文