C# 如何将此sql转换为efcore linq
原始数据表: 可以获得所需结果的SQL:C# 如何将此sql转换为efcore linq,c#,linq,C#,Linq,原始数据表: 可以获得所需结果的SQL: 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>()
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")
};
执行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")
};
var hAATGroups=来自dbContext.Set()中的p
其中p.type==“1”
按新的{group\U number=p.group\U number,Receive\U date=p.Receive\U date}将p分组到dto中
选择新DTO_HAATGroup
{
组号=dto.Key.Group号,
HAAT_count=dto.count(),
Receive_date=dto.Key.Receive_date.ToString(“yyyy-MM-dd”)
};
很难相信关于转换的问题,当convert from code包含whhere
时,请提供sql语句和linq语句的示例输入和输出我认为您需要依赖dto
而不是a
的值,为什么要使用.Set()
而不是正确的DbSet属性?如果不配置实体,甚至不能使用Set()
,那么这有什么意义呢?至于使用字符串表示日期,这是一个主要的数据库设计缺陷String.ToString()
只返回字符串本身,因此Sampling\u date.ToString(“yyyy-MM-dd”)
毫无意义,甚至不会编译您的实际代码、实际表架构、样本数据和预期结果。文本,而不是图像。截图与代码不匹配。代码无法编译。无法复制和执行图像,因为您输入的代码中没有Receive\u date
属性posted@PanagiotisKanavos Receive_date实际上是HS_HZXX实体中的一个属性。当然,它还包含一个Sampling_date属性,我以前犯过一个错误。主要解决count()的问题。谢谢你的回复。
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")
};