簡體   English   中英

如何將此 sql 轉換為 efcore linq

[英]How to convert this sql to efcore 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>()
                         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")
                         };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM