如何返回动态类型 List<dynamic>使用 Dapper ORM [英] How to return dynamic types List&lt;dynamic&gt; with Dapper ORM

查看:53
本文介绍了如何返回动态类型 List<dynamic>使用 Dapper ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Dapper.net 一段时间了,它是一个非常好的 ORM 映射器,适用于 .Net 动态类型.

I have been using Dapper.net for a while now and its a very good ORM mapper which works great with .Net dynamic types.

但我注意到,当 Dapper 从数据库中检索数据时,它以 DapperRow 类型返回.

But I noticed that when Dapper retrieves data from a database it returns as DapperRow type.

有没有什么办法可以像System.Dynamic.ExpandoObject那样以任何其他类型返回它?

Is there are any way that I can return it in any other type Like System.Dynamic.ExpandoObject?

推荐答案

DapperRow 对象旨在在行之间共享大量状态.例如,如果您获取 40 行,列名等只会存储一次.如果我们使用 ExpandoObject,则需要按行进行配置.因此,使用 DapperRow 作为幕后实现细节是一种有意的效率.

The DapperRow object is designed to share a lot of state between rows. For example, if you fetch 40 rows, the column names etc are only stored once. If we used ExpandoObject, this would need to be configured per row. Hence, the use of DapperRow as the behind-the-scenes implementation detail is a deliberate efficiency thing.

请注意,从 dynamic API 返回的对象也可以转换为 IDictionary.

Note that the object returned from the dynamic APIs can also be cast as IDictionary<string,object>.

但是,我愿意支持支持此字典用法的其他类型 - ExpandoObject 就是其中之一.所以是的,它可以改变为:

I would, however, be open to supporting other types that support this dictionary usage - of which ExpandoObject is one. So yes, it could be changed such that:

var rows = conn.Query<ExpandoObject>(...);

有效.它只需要代码来支持它,而该代码目前不存在.所以不,但也许在未来的版本中".

works. It simply requires code to support it, and that code does not currently exist. So "no, but perhaps in a future build".

另请注意,您根本不需要使用 DapperRow...更预期的场景是使用通用 API 来具体化您自己的类型.

Note also that you don't need to use DapperRow at all... The more expected scenario is to use the generic API to materialize your own types.

这篇关于如何返回动态类型 List<dynamic>使用 Dapper ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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