我怎样才能让我的 dapper 结果成为一个列表?

How can I get my dapper result to be a List?

为什么我不能在上面添加 .ToList()? Intellisense 唯一允许的是 .ToString().

//..
string sqlQuery = "SELECT sum(SellingPrice) as SellingPrice, sum(MarkupPercent) as MarkupPercent, sum(MarkupAmount) as MarkupAmount FROM ProfitMargins WHERE QuoteId in @QuoteId group by multiplier";
{
    List<ProfitMargin> profitMargin = (List<ProfitMargin>)await conn.QueryAsync<List<ProfitMargin>>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})  //would like to add .ToList() here;

    return profitMargin;
}
//..

更新

我认为问题与 conn.queryasync(conn 是 context.Database.Connection.ConnectionString)有关,而不是 context.Database.SqlQuery

试试改成这个。

List<ProfitMargin> profitMargin = (await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})).ToList();

或者

var results = await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()});
List<ProfitMargin> profitMargin = results.ToList();

我认为您在尝试调用 .ToList()

时遇到了 Task 对象

尝试:

List<ProfitMargin> profitMargin = new List<ProfitMargin>(await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}));

List<ProfitMargin> profitMargin = (await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})).ToList();

如@Amy 所述,您需要使用

conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}))

which returns a Task<IEnumerable<ProfitMargin>> 因此在等待时评估为 IEnumerable<ProfitMargin>.