在 sql 中编写一个类似的 LINQ 查询来聚合非重复计数?
我想获得每个月的计数,但即使发生多次,计数也只能每天最多一次。我的 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。谢谢!
I want to get a count for each month but count should be only at most one per day even if there are multiple occurences . I have the SQL query which works right but having trouble to convert it into 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)
Could anyone help me translating the above query to LINQ. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 使用 GROUP BY 和 COUNT(DISTINCT) 的 LINQ to SQL由@Rick给出,这应该有效:
我不知道L2S是否可以转换内部g.Select(act =>; act.Date.Day).Distinct.Count()
正确。Per LINQ to SQL using GROUP BY and COUNT(DISTINCT) given by @Rick, this should work:
I don't know if L2S can convert the innerg.Select(act => act.Date.Day).Distinct.Count()
properly.