sql - 如何在linq中计算多列数据
问题描述
SELECT country, city, COUNT(school), COUNT(collage)
FROM T1
GROUP BY country, city
我在 Linq 中写了这个查询
var count_captainRegisterd1 = (from a in dt1.AsEnumerable()
group a by new
{
oc_name = a.Field<string>("county"),
oc_city = a.Field<string>("city")
} into g
select new
{
g.Key.oc_name,
g.Key.oc_city,
country = g.Count()
}
).ToList();
但他们算country
列。我想要country
,city
两者都算
解决方案
这是您查询的 linq:
var count_captainRegisterd1 =
(from a in dt1.AsEnumerable()
group a by new
{
oc_name = a.Field<string>("county"),
oc_city = a.Field<string>("city")
} into g
select new
{
Country = g.Key.oc_name,
City = g.Key.oc_city,
SchoolCount = g.Count(r => !string.IsNullOrWhiteSpace(r.Field<string>("school"))),
CollageCount = g.Count(r => !string.IsNullOrWhiteSpace(r.Field<string>("collage")))
}).ToList();
推荐阅读
- android - 如何在 Android Cloud Firestore 中下载部分文档数据?
- python - Python3.7光栅库打不开jp2
- passport.js - 如何从身份提供者导入元数据文件?
- node.js - 动态数据未使用 NodeJS 通过 sendgrid web api 发送
- azure - 如何在 Azure 门户中为高级存储帐户的 Azure Blob 服务设置 CORS?
- python - Databricks 命令说从 Python 运行时未配置 databricks-cli(使用 os.system()),但在粘贴到命令行时工作正常
- flutter - 如何在vscode中为Flutter设置logo的Foreground layer和Background Layer?
- python - 如何冻结多个相关类方法的一些参数
- git - Git-LFS 添加文件类型(不加倍 repo 大小)
- python - rpy2应用函数后数据框没有变化