[英]In Dapper, what does the Func<TFirst, TSecond, TReturn> map method parameter looks like. Is there a test case that I can look at?
I have been trying to implement the below dapper method call. 我一直在尝试实现以下dapper方法调用。 I do not know what is the expectation for the method parameter: 我不知道对方法参数的期望是什么:
Func<TFirst, TSecond, TReturn> map
for this Dapper call: 对于此Dapper调用:
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. 我还没有找到。
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. 首先,这看起来像是一个重载,否则QueryAsync的工作方式与Query相同,只是异步的。 This would have made more sense for QueryMultiple
这对于QueryMultiple
更有意义
Important parts are: 重要部分是:
QueryAsync<TFirst, TSecond, TReturn>
- Signature QueryAsync<TFirst, TSecond, TReturn>
-签名
Func<TFirst, TSecond, TReturn> map
- Usage of Signature Func<TFirst, TSecond, TReturn> map
-签名的用法
Usage of Signature 签名使用
Task<IEnumerable<TReturn>>
- Return Type Task<IEnumerable<TReturn>>
-返回类型
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: 现在,由于您正在执行Query
而不是QueryMultiple
,因此Sql Execution的返回将仅为单一类型,我假设TReturn
,Func委托将要做的是,对结果进行一些处理,这需要通过提供逻辑,例如TFirst和TSecond变为整数类型,它可以是您选择的任何类型:
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. 我必须承认我有点困惑,因为在QueryAsync
函数中执行任何操作都意味着解包Task
,这是从Async函数返回的。 Better solution here shall be: 更好的解决方案是:
Run QueryAsync as is: 按原样运行QueryAsync:
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>
现在应用Func
转换IEnumerable<TReturn>
的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.