How to use Dapper to return a list of dynamic types (list)?

.net c# dapper orm

Question is a wonderful ORM mapper that works well with, and I've been using it for a long. dynamic type nets.

However, I discovered that Dapper returns data from databases asDapperRow type.

Is there any way I can return it in a different format? LikeSystem.Dynamic.ExpandoObject ?

5/30/2019 6:43:50 AM

Accepted Answer

The DapperRow The object is built such that rows may exchange a lot of state. For instance, if you get 40 rows, just the values once are kept for column names, etc. If applied,ExpandoObject This would need to be set up for each row. so that the usage ofDapperRow as the implementation detail used in the background was done on purpose to increase efficiency.

The object returned by thedynamic Moreover, APIs may be cast asIDictionary<string,object> .

However, I would be willing to accept other kinds that do enable this dictionary use.ExpandoObject is one. Such, definitely, it should be altered so that:

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

works. It merely needs a piece of code that doesn't yet exist to support it. Therefore, "no, but maybe in a later build."

Also keep in mind that you don't have to utilizeDapperRow at all... The more expected situation is to materialize your own types using the generic API.

5/30/2019 6:45:18 AM

Popular Answer


Use the query technique to acquire your dynamics in accordance with the dapper documentation:

dynamic account = conn.Query<dynamic>(@"
                    SELECT Name, Address, Country
                    FROM Account
            WHERE Id = @Id", new { Id = Id }).FirstOrDefault();

As you can see, as long as the attributes are well described in the query statement, you get a dynamic object and may use it.

If omitted.FirstOrDefault() You obtain aIEnumerable<dynamic> which you are free to use whatever you like.

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow