简体   繁体   English

如何将此 sql 转换为 efcore linq

[英]How to convert this sql to efcore linq

Original data sheet:原始数据表:

在此处输入图像描述

SQL that can get the result you want: SQL 可以得到你想要的结果:

select group_number,count(group_number) as group_count,receive_date 
from hS_HZXX 
where type = '1' 
group by group_number,receive_date;

在此处输入图像描述

use ef core linq like this:像这样使用 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")
                         };

The DTO_HAATGroup: DTO_HAAT 组:

public class DTO_HAATGroup
{

    public string Group_number { get; set; }
    public int HAAT_count { set; get; }
    public string Sampling_date { get; set; }

}

The result of executing linq:执行linq的结果:

在此处输入图像描述

But I got the wrong result.但我得到了错误的结果。 . . . . Can you help me?你能帮助我吗? How can I convert this SQL into a correct Linq statement.如何将此 SQL 转换为正确的 Linq 语句。

I have solved this problem, the correct Linq writing is as follows:这个问题我已经解决了,正确的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")
                         };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM