如何使我的dapper结果成为列表?

问题描述 投票:0回答:2
为什么我不能添加

.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; } //..

update

我认为问题与conn.queryAsync(conn是context.database.connection.connectionstring)而不是context.database.sqlquery

trory改变了这一点。
c# dapper
2个回答
31
投票

or

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

我认为您正在尝试打电话

Task

    

try:
.ToList()

or

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

当@amy说,您需要使用
List<ProfitMargin> profitMargin = (await conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()})).ToList();

在等待评估时返回
conn.QueryAsync<ProfitMargin>(sqlQuery, new { QuoteId = QuoteIds.ToArray()}))

是以前答案的补充:

从C#12中,也可以使用收集表达式。这简化了转换为:

Task<IEnumerable<ProfitMargin>>
    

© www.soinside.com 2019 - 2025. All rights reserved.