我想获得每个月的计数,但即使有多次出现,每天计数最多也只能是一个。我有正确的SQL查询,但是无法将其转换为LINQ-
select
count(DISTINCT DAY(date)) as Monthly_Count,
MONTH(date) as Month,
YEAR(date)
from
activity
where
id=@id
group by
YEAR(date),
MONTH(date)
谁能帮我将上面的查询翻译成LINQ。谢谢!
最佳答案
@Rick给出的LINQ to SQL using GROUP BY and COUNT(DISTINCT)可以正常工作:
var query = from act in db.Activity
where act.Id == id
group act by new { act.Date.Year, act.Date.Month } into g
select new
{
MonthlyCount = g.Select(act => act.Date.Day).Distinct().Count(),
Month = g.Key.Month,
Year = g.Key.Year
};
我不知道L2S是否可以正确转换内部
g.Select(act => act.Date.Day).Distinct.Count()
。关于c# - 写一个可比较的LINQ查询以求sql中的总非重复计数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6836119/