c# - 如何将此 sql 转换为 efcore linq
问题描述
原始数据表:</p>
可以得到你想要的结果的SQL:</p>
select group_number,count(group_number) as group_count,receive_date
from hS_HZXX
where type = '1'
group by group_number,receive_date;
像这样使用 ef core linq:
var hAATGroups = from p in dbContext.Set<HS_HZXX>()
where p.type == "1"
group p by new { Group_number = p.group_number, Sampling_date = p.sampling_date } into dto
select new DTO_HAATGroup
{
Group_number = dto.Key.Group_number,
HAAT_count = dto.Key.Group_number.Count(),
Sampling_date = dto.Key.Sampling_date.ToString("yyyy-MM-dd")
};
DTO_HAAT 组:
public class DTO_HAATGroup
{
public string Group_number { get; set; }
public int HAAT_count { set; get; }
public string Sampling_date { get; set; }
}
执行linq的结果:
但我得到了错误的结果。. . 你能帮助我吗?如何将此 SQL 转换为正确的 Linq 语句。
解决方案
我已经解决了这个问题,正确的Linq写法如下:
var hAATGroups = from p in dbContext.Set<HS_HZXX>()
where p.type == "1"
group p by new { Group_number = p.group_number, Receive_date = p.receive_date } into dto
select new DTO_HAATGroup
{
Group_number = dto.Key.Group_number,
HAAT_count = dto.Count(),
Receive_date = dto.Key.Receive_date.ToString("yyyy-MM-dd")
};
推荐阅读
- c# - 在 Unity 对象中切换场景时 100 倍的数量
- c# - 在自定义模型验证器 .NET Core 中格式化错误消息
- laravel - 抱歉,找不到您要找的页面 laravel
- multithreading - Rust 中的共享互斥锁问题(为 Arc 实现 AsyncRead/AsyncWrite
>) - amazon-web-services - aws ApiGateway 部署到特定阶段
- c++ - POSIX 线程的并行程序比串行程序慢
- react-admin - react-admin 编辑等待响应
- node.js - Nodejs 对象 - 如何同时添加值和定义新属性?
- reactjs - 在另一个库中时反应组件道具更改不会触发 useEffect()
- r - 使用 rbind 或 do.call 在 R 中的 for 循环问题