In Dapper, what does the Func map method parameter looks like. Is there a test case that I can look at?

c# dapper delegates

Question

I have been trying to implement the below dapper method call. I do not know what is the expectation for the method parameter:

Func<TFirst, TSecond, TReturn> map

for this Dapper call:

Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))

Is there a test case that I can take a look at? I have not found one yet.

Popular Answer

Let's understand in little detail from the signature provided. Firstly this looks like an overload, otherwise the QueryAsync works in same way as Query, just that its Asynchronous. This would have made more sense for QueryMultiple

Important parts are:

QueryAsync<TFirst, TSecond, TReturn> - Signature

Func<TFirst, TSecond, TReturn> map - Usage of Signature

Usage of Signature

Task<IEnumerable<TReturn>> - Return Type

Now since you are executing the Query, not QueryMultiple, so the return of the Sql Execution would be only single type, I assume TReturn, what Func delegate would do is, do some processing of the result, which you need to do by supplying the logic, something like, where TFirst and TSecond become integer types, it could be any type of your choice:

Func<int,int,TReturn> func = (a,b) => { use a,b values to filter the values in IEnumerable<Treturn> fetched and return the same }

I must admit that I am little confused though, since for doing anything inside the QueryAsync function means unwrapping the Task, which is returned from the Async function. Better solution here shall be:

Run QueryAsync as is:

Task<IEnumerable<TReturn>> QueryAsync<TReturn>(string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))

var result = connection.QueryAsync<TReturn>(sql).Result; // Making this as a blocking call just for example

Now apply the Func to transform the values inside IEnumerable<TReturn>



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why