簡體   English   中英

將T-SQL轉換為LINQ Lambda

[英]Convert T-SQL into LINQ Lambda

如果需要,如何用LINQ lambda表達式或查詢語法編寫此查詢? 我已經嘗試了一段時間了。 多次連接使它變得困難。 該聯接的總記錄超過2000,最終結果應為15條記錄

select 
    year([date]), 
    month([date]),
    sum(ot.rate)
from s as s 
join cs cs on cs.s_id= s.id
join ot ot on ot.id = cs.o_id
group by
    year([date]), 
    month([date])

這是我所獲得的最接近的,但不會編譯。 我無法從select塊中訪問ot屬性。

    var query = from se in table_se
        join cs in table_cs on se.Id equals cs.S_Id
        join ot in table_ot on cs.O_Id equals ot.Id
        group se by new { se.Date.Year, se.Date.Month } into grp 
        select new
        {
            grp.Key.Year,
            grp.Key.Month,
            grp.Sum(ot.Rate)
        };

您非常接近Enumerable.Sum帶有謂詞

var query = from se in table_se
        join cs in table_cs on se.Id equals cs.S_Id
        join ot in table_ot on cs.O_Id equals ot.Id
        group ot by new { se.Date.Year, se.Date.Month } into grp 
        select new
        {
            Year = grp.Key.Year,
            Month = grp.Key.Month,
            Sum = grp.Sum(x => x.Rate)
        };

暫無
暫無

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

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