1

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

louuuuuu
  • 13
  • 4

1 Answers1

1

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
                 }
             );
Milney
  • 6,253
  • 2
  • 19
  • 33
  • I knew about await but forgot to put it back before posting the question. But adding await back + the extra pair of parantheses did make a difference though! at least it's not complaining about the select now; new error says "Product does not contain definition for "fldCode" or something. One step forward, thanks! :) – louuuuuu Sep 01 '20 at 09:18
  • Note that since you are querying for a product (i.e. the bit), Dapper will already be mapping the columns to that type. If your stored procedure returns a field called 'Code' it will go into the 'Code' property on the Product object that is returned. You can't map the fields in the manner you are trying to do it here. Either change your SPROC to return the column called 'Code' instead of 'fldCode', or change your Product class to have 'fldCode' property instead of 'Code', and Dapper will map it for you, you don't need the Select – Milney Sep 01 '20 at 09:22
  • Didn't want to change the column names but I will try this anyway, thanks! – louuuuuu Sep 01 '20 at 09:49
  • You can also make a new class called like ProductQueryResult with the fldCode etc, then do QueryAsync then your select will work – Milney Sep 01 '20 at 09:50