Search code examples
c#linqcompiler-errorsdapper

c# Dapper - using linq on QueryAsync method


Why can't I use .select on a queryAsync when it works fine on Query? For example, here the .select yells at me telling me it cant resolve the symbol "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. if anything it has trouble resolving the fldCode columns from the sp:

 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'"


Solution

  • 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
                     }
                 );