在 sql 中编写一个类似的 LINQ 查询来聚合非重复计数?

发布于 2024-11-26 14:49:09 字数 301 浏览 1 评论 0原文

我想获得每个月的计数,但即使发生多次,计数也只能每天最多一次。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2024-12-03 14:49:09

根据 使用 GROUP BY 和 COUNT(DISTINCT) 的 LINQ to SQL由@Rick给出,这应该有效:

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() 正确。

Per LINQ to SQL using GROUP BY and COUNT(DISTINCT) given by @Rick, this should work:

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
            };

I don't know if L2S can convert the inner g.Select(act => act.Date.Day).Distinct.Count() properly.

我为君王 2024-12-03 14:49:09
var results = db.activities.Where(a => a.id == myID)
                           .GroupBy(a => new 
                                         {
                                             Month = a.date.Month, 
                                             Year = a.date.Year
                                         })
                           .Select(g => new
                                        {
                                            Month = g.Key.Month,
                                            Year = g.Key.Year,
                                            Monthly_Count = g.Select(d => d.date.Day)
                                                             .Distinct()
                                                             .Count()
                                        })
var results = db.activities.Where(a => a.id == myID)
                           .GroupBy(a => new 
                                         {
                                             Month = a.date.Month, 
                                             Year = a.date.Year
                                         })
                           .Select(g => new
                                        {
                                            Month = g.Key.Month,
                                            Year = g.Key.Year,
                                            Monthly_Count = g.Select(d => d.date.Day)
                                                             .Distinct()
                                                             .Count()
                                        })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文