Dapper Results (Dapper Row) with parenthesis

According to the Dapper documentation, you can get a dynamic list from dapper using the following code:

var rows = connection.Query("select 1 A, 2 B union all select 3, 4"); ((int)rows[0].A) .IsEqualTo(1); ((int)rows[0].B) .IsEqualTo(2); ((int)rows[1].A) .IsEqualTo(3); ((int)rows[1].B) .IsEqualTo(4); 

What is the use of dynamic if you need to know the field names and data types of the fields. If I have:

 var result = Db.Query("Select * from Data.Tables"); 

I want to do the following: Get a list of returned field names and data types.

Loop through it using the field names and get the data in the following ways:

 result.Fields ["Id", "Description"] result[0].values [1, "This is the description"] 

That would allow me to get

 result[0].["Id"].Value 

which will give results 1 and will, for example, be of type Int 32

 result[0].["Id"].Type --- what datattype is the value returned result[0].["Description"] 

which will produce the results of "This Description" and will be of type string.

I see that there is a results [0] .table file that has a dapperrow object with an array of field names, and there is also a result.values ​​file that is an object [2] with values ​​in it, but it cannot be accessed. If I add a clock to the column name, I can get the identifier. Automatically created watches:

 (new System.Collections.Generic.Mscorlib_CollectionDebugView<Dapper.SqlMapper.DapperRow>(result as System.Collections.Generic.List<Dapper.SqlMapper.DapperRow>)).Items[0].table.FieldNames[0] "Id" string 

So I should be able to get the result [0] .Items [0] .table.FieldNames [0] and return "Id".

+9
source share
1 answer

You can attribute each line to an IDictionary<string, object> , which should provide access to names and values. We clearly don't track types at the moment - we just don't need to. If this is not enough, consider using the dapper method, which returns IDataReader - this will provide access to the raw data, but at the same time allow the use of convenient call / parameterization syntax.

For instance:

 var rows = ... foreach(IDictionary<string, object> row in rows) { Console.WriteLine("row:"); foreach(var pair in row) { Console.WriteLine(" {0} = {1}", pair.Key, pair.Value); } } 
+20
source

Source: https://habr.com/ru/post/978192/


All Articles