我想將此 SQL 查詢轉換為 C# 中的 Linq:
select
count(*),
FORMAT (CreatedDate, 'MM') as months,
FORMAT (CreatedDate, 'yyyy') as year
from
ProviderPosts
group by
FORMAT (CreatedDate, 'MM'),
FORMAT (CreatedDate, 'yyyy')
uj5u.com熱心網友回復:
以下代碼幾乎是您查詢的精確轉換,但是根據 LINQ 提供程式,它可能無法將其轉換為 SQL。
from pp in ProviderPosts
group pp by new DateTime(pp.CreatedDate.Year, pp.CreatedDate.Month, 1) into g
select new {
Count = g.Count(),
months = g.Key.ToString("MM"),
year = g.Key.ToString("yyyy"),
}
在 SQL 中,按
EOMONTH或DATEFROMPARTS而不是分組更有效FORMAT
uj5u.com熱心網友回復:
盡量不要轉換成任何東西。使用.Year和.Month
var query =
from pp in ProviderPosts
group pp by new { pp.CreatedDate.Year, pp.CreatedDate.Month } into g
select new
{
Count = g.Count(),
g.Key.Month,
g.Key.Year,
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318196.html
上一篇:linqgroupby和最大計數
