c# - 如何在 EF 6 中使用原始 sql 查询循环遍历行?
问题描述
如何从下面的查询中获取值并循环它们?查询返回一个计数。
var quantities = db.Database.SqlQuery<List<string>>
(@"SELECT Quantity
FROM Runlist WHERE(UserId = @userid)
GROUP BY Quantity", new SqlParameter("@userid", user.Id)).ToList();
foreach (var qty in quantities)
{
}
这是查询的结果。
Quantity
---------
1250
1750
2500
5000
5250
6250
11500
12250
12500
15500
17000
164250
解决方案
像这样替换您的查询以获得数量的计数
List<string> quantities = new List<string>();
quantities = db.Database.SqlQuery<List<string>>
(@"SELECT Quantity
FROM Runlist WHERE(UserId = @userid)
GROUP BY Quantity", new SqlParameter("@userid", user.Id)).ToList();
foreach (var qty in quantities)
{
// get value here
}
推荐阅读
- arrays - [SwiftUI]:ForEach 不适用于字典数组、带数组的字典
- javascript - 如何在 npm 中阻止广告?
- ionic4 - 如何为视频创建保留,使用相机或其他插件在 Ionic 4/5 中点击类似 whatsapp 功能的照片
- angular - 如何在远程 Tomcat 服务器中渲染 Angular Universal
- node.js - 将 Express 与 Google Cloud Functions 结合使用时,如何监控每个端点的执行时间?
- javascript - jQuery,如何设置输入是多于一个输入的通知
- c# - 单击 Xamarin.Forms 中的 ListView 项后完整显示详细信息
- php - 如何从 URL 中获取包含的用户名和密码
- android - 带有 OutlinedBox 笔划的 TextInputLayout 与提示重叠
- sql - 在 Oracle 中使用 JOINS 插入