c#Dapper-QueryAsyncメソッドでlinqを使用

Sep 01 2020

Queryで正常に機能するのに、queryAsyncで.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
                            });

しかし、クエリに変更するとすぐに、もう怒鳴りません。どちらかといえば、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が列をエンティティプロパティにマッピングします-「シンボル「選択」を解決できません」

回答

1 Milney Sep 01 2020 at 09:06

非同期メソッドはタスク(基本的には非同期アクションの最終結果のプレースホルダー)を返します。実際の値を取得するには、それを待つ必要があります

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