linq - LINQ 方法查询中的多选问题
问题描述
我们有这个 TSQL:
SELECT 'Outdoor Tournaments' AS Name, COUNT(a.location) AS Value
FROM
(select location
from [dbo].[TournamentBatchItem]
GROUP BY TournamentName, location) a
WHERE a.location = 'Outdoor'
UNION
SELECT 'Indoor Tournaments' AS Name, COUNT(a.location) AS Value
FROM
(select location
from [dbo].[TournamentBatchItem]
GROUP BY TournamentName, location) a
WHERE a.location = 'Indoor'
LINQ 方法的初始解决方案:
var tournamentStats1 = await _context.TournamentBatchItem.Where(t => t.Location == "Outdoor" || t.Location == "Indoor")
.GroupBy(t => t.Location)
.Select(t => new { Name = t.Key, Value = t.Location})
.ToListAsync();
然后我们意识到这不是正确的,我们对此进行了修改并测试了 LinqPAD应用程序并运行正常:
var tournamentStats1 = await _context.TournamentBatchItem.Where(t => t.Location == "Outdoor" || t.Location == "Indoor")
.GroupBy(t => new { t.TournamentName, t.Location })
.Select(t => new { Name = t.Key.TournamentName, Value = t.Key.Location })
.GroupBy(a => a.Value)
.Select(a => new Stat { Name = a.Key, Value=a.Count() })
.ToList();
但是,当我们放入 .Net Core 2.1 Web 应用程序时,我们有这个“System.ArgumentException: must be reducible node”,它看起来与嵌套选择有关,所以我们通过拆分查询将其修改为下面。这个工作正常。
var tournamentStats1 = await _context.TournamentBatchItem.Where(t => t.Location == "Outdoor" || t.Location == "Indoor")
.GroupBy(t => new { t.TournamentName, t.Location })
.Select(t => new { Name = t.Key.TournamentName, Value = t.Key.Location })
.ToListAsync();
var tournamentStats = tournamentStats1.GroupBy(a => a.Value)
.Select(a => new Stat { Name = a.Key, Value=a.Count() })
.ToList();
这是一个错误吗?有人有这个 2.1 版本的经验吗?
谢谢
解决方案
推荐阅读
- python - 为什么 repaint() 可以正常工作,但 update() 不能?
- angular - 为什么我的 HTML 元素内容的角度删除属性?
- python - 如何使用 Python 从 Outlook 帐户发送带有附件的邮件
- python - 将视频转换为帧 python
- python-3.x - 使用脚本(最好是 Python3 或 Bash)安装软件后在 Ubuntu 中自动添加别名
- python-3.x - 熊猫解析具有时区偏移的日期不返回日期时间的dtype,只是对象?
- java - 使用 FlatFileItemReader 读取元素列表
- python - python:object()不带参数错误
- python - 布尔切片交换 numpy 数组的维度
- javascript - 将 nodetext 附加到 svg 元素