How to Deal With Dapper return Key Value pair with Dynamic from LINQ
Asked Answered
P

1

5

I am new to Dapper. Let me explain what i am trying to achieve. I have simple query which is returning columns from DB, i don't want to create Entity(Property) class for it to fill the data. I want to use Dynamic Type for this. Below is my code:

public dynamic GetABC(int year)
    {

        using (var db = new MySql.Data.MySqlClient.MySqlConnection(ClsConnectionString.connectionString))
        {

            string query = string.Format(@"SELECT * FROM ranking where YEAR(eventdate)='{0}') ORDER BY eventdate DESC, eventid ASC",year);
            //return db.Query<RankingEntity>(query, commandType: System.Data.CommandType.Text).AsList();
            var a = (IEnumerable<dynamic>)db.Query(query, null, null, true, null, CommandType.Text).ToList();
            return a;
       }
    }

It returns me Key Value pair. like below: Dapper Row

Is there any way, i just write one common method to get List as "Property" = "Value" As normally we get when we use any Entity Class.

I am not sure, if it is achievable but i wish to learn if it is possible and want to implement in our application.

I can achieve this by implementing loop through the dapperrows which i don't want. because if there will be 10000 rows loop will iterate 10000 times.

Thanks in advance.

Poree answered 3/4, 2017 at 5:48 Comment(2)
DapperRow is an internal class of the Dapper, which can be directly type- casted into IDictionary<string,object>, which has a key string and value objectAstolat
@MrinalKamboj, could you please update the above code to get this. it would be better for to understandPoree
A
7

As I have mentioned DapperRow is an internal class of the Dapper, which can be directly typecast to IDictionary<string,object>, since it implements IDictionary interface. All that you have to do is:

var a = db.Query(query, null, null, true, null, CommandType.Text).Select(x => x as IDictionary<string,object>);

Now result would be of type IEnumerable<IDictionary<string,object>>, which can be used to extract all the required information, since column names are keys and respective row value is the value of each element in the IEnumerable.

Also check, this may too work, but I am not sure:

 var a = (IEnumerable<IDictionary<string,object>>)db.Query(query, null, null, true, null, CommandType.Text);
Astolat answered 3/4, 2017 at 6:6 Comment(6)
It returns as same DapperRow. as above.Poree
You can typecast, DapperRow is only for the debuggerAstolat
TypeCast to my Entity Class?Poree
No Typecast to IDictionary<string,object> as I have shown, then you have to work at creating your typed entityAstolat
What's the problem in doing so, its straight forward, in case you already know type then use Query<T>, otherwise I don't see much choice hereAstolat
Because sometimes, we need less columns sometimes more.. in that case we have to take all properties in account.. that's whyPoree

© 2022 - 2024 — McMap. All rights reserved.