简体   繁体   English

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

[英]c# Dapper - using linq on QueryAsync method

Why can't I use .select on a queryAsync when it works fine on Query?当 .select 在 Query 上运行良好时,为什么我不能在 queryAsync 上使用它? For example, here the .select yells at me telling me it cant resolve the symbol "select":例如,这里 .select 对我大喊大叫,告诉我它无法解析符号“select”:

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

but as soon as i change to Query, it doesn't yell anymore.但是一旦我更改为 Query,它就不再大喊大叫了。 if anything it has trouble resolving the fldCode columns from the sp:如果有任何问题,它在解析 sp 中的 fldCode 列时遇到问题:

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

why is this?为什么是这样?

I asked a previous question here C# Dapper mapping columns to entity properties - "Cannot resolve symbol 'Select'"我在这里问了一个以前的问题C# Dapper 将列映射到实体属性 - “无法解析符号‘选择’”

Async methods return a Task (basically a placeholder for the eventual result of the async action).异步方法返回一个任务(基本上是异步操作最终结果的占位符)。 You need to await it to get the actual value您需要等待它才能获得实际值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM