查询多个结果,使点dapper as dapper
我正在尝试使用现有代码解决问题,并且不确定此方法是否正确,因为我以前从未与Dapper合作。我正在尝试使用存储过程返回对象列表。
下面的代码编译,但我不确定这是否是返回多行的正确方法。是否有什么可以返回列表,而不是我循环循环并构建列表然后返回?
public async Task<List<Event>> GetEventsAsync()
{
await using SqlConnection db = new(this.settings.ConnectionString);
List<Event> events = new List<Event>();
var eventsMarkedPublished = await db.QueryAsync<Event>("[sp_get_published_events]", null, commandTimeout: 60, commandType: CommandType.StoredProcedure);
foreach (var pubEvents in eventsMarkedPublished)
{
events.Add(pubEvents);
}
return events;
}
I am trying to fix an issue with an existing code and not sure if this approach is correct or not as I have not worked with dapper before. I am trying to return a list of objects using a stored procedure.
The code below compiles but I am not sure if this is the correct way to return multiple rows or not. Is there anything that just returns to list rather than me looping through and constructing list and then returning?
public async Task<List<Event>> GetEventsAsync()
{
await using SqlConnection db = new(this.settings.ConnectionString);
List<Event> events = new List<Event>();
var eventsMarkedPublished = await db.QueryAsync<Event>("[sp_get_published_events]", null, commandTimeout: 60, commandType: CommandType.StoredProcedure);
foreach (var pubEvents in eventsMarkedPublished)
{
events.Add(pubEvents);
}
return events;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以直接尝试通过
TOLIST
方法返回list
。或者
We can try to return
List
byToList
method directly.or