c# Dapper - 在 QueryAsync 方法上使用 linq

标签 c# linq compiler-errors dapper

为什么我不能在 Query 上正常使用 .select 时在 Query 上使用它?
例如,这里 .select 对我大喊大叫,告诉我它无法解析符号“select”:

var result = await sqlConnection.QueryAsync<Product>(procedure,
                            commandType: CommandType.StoredProcedure).Select(p => new Product
                        {
                            Code= (string)p.fldCode,
                            Name=(string)p.fldName
                            });
但是一旦我更改为查询,它就不再大喊大叫了。如果有任何问题,它无法从 sp 解析 fldCode 列:
 var result = sqlConnection.Query<Product>(procedure,
                                commandType: CommandType.StoredProcedure).Select(p => new Product
                            {
                                Code= (string)p.fldCode,
                                Name=(string)p.fldName
                                });
为什么是这样?
我在这里问过一个问题 C# Dapper mapping columns to entity properties - "Cannot resolve symbol 'Select'"

最佳答案

异步方法返回一个任务(基本上是异步操作最终结果的占位符)。您需要等待它才能获得实际值

var result = (await sqlConnection.QueryAsync<Product>(
                 procedure,
                 commandType: CommandType.StoredProcedure
             )).Select(p => new Product
                {
                    Code= (string)p.fldCode,
                    Name=(string)p.fldName
                 }
             );

关于c# Dapper - 在 QueryAsync 方法上使用 linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63684387/

相关文章:

swift - 由于信号 : Segmentation fault: 11 | Xcode 7. 2 命令失败

pdf - 导入 *.pdf_tex 文件错误

c# - 将 VB6 随机化转换为 C#

c# - 使用FileSystemWatcher检测xml文件,使用Linq读取xml文件提示结果错误 "Root Element is Missing"

c# - 转换为值类型 'Int32' 失败的空值 LINQ

c# - 在 LINQ 之外使用匿名类型是一件好事吗?

VB.NET:运算符 '=' 未定义...用于相同类型的变量和对象?

c# - 评估 n 个变量之间的相等性,而不检查每一对

c# - Entity Framework 多对多数据不持久

c# - 如何使用 Dapper 检查空值