如何才能将我的精巧结果变为列表?

时间:2016-09-20 18:51:46

标签: c# dapper

为什么我无法在此添加.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

有关。

2 个答案:

答案 0 :(得分:9)

尝试更改为此。

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

或者

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

我认为您试图调用Task

时,您正在点击.ToList()个对象

答案 1 :(得分:0)

尝试:

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()}))在等待评估Task<IEnumerable<ProfitMargin>>时返回IEnumerable<ProfitMargin>