在 Dapper 中,Func<TFirst、TSecond、TReturn> map 方法参数是什么样的。是否有我可以查看的测试用例?



我一直在尝试实现下面的dapper方法调用。我不知道对方法参数的期望是什么:

Func<TFirst, TSecond, TReturn> map

对于此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?))

有没有一个测试用例我可以看看?我还没有找到。

让我们从提供的签名中了解一些细节。首先,这看起来像是一个重载,否则QueryAsync的工作方式与Query相同,只是它是异步的。这对QueryMultiple 更有意义

重要部件包括:

QueryAsync<TFirst, TSecond, TReturn>-签名

Func<TFirst, TSecond, TReturn> map-签名的使用

签名的使用

Task<IEnumerable<TReturn>>-返回类型

现在,由于您正在执行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 }

不过,我必须承认我有点困惑,因为在QueryAsync函数内部执行任何操作都意味着打开从Async函数返回的Task。更好的解决方案是:

按原样运行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;//将其作为阻塞调用,例如

现在应用Func来转换IEnumerable<TReturn> 内的值

相关内容

最新更新