简体   繁体   中英

How to convert this sql to efcore linq

Original data sheet:

在此处输入图像描述

SQL that can get the result you want:

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:

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:

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:

在此处输入图像描述

But I got the wrong result. . . Can you help me? How can I convert this SQL into a correct Linq statement.

I have solved this problem, the correct Linq writing is as follows:

        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")
                         };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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