Group后统计数据问题

TongJL1993 2017-02-07 10:55:17
测试数据:
统计的代码:
结果数据:
板块分组后统计主题发表人数!同1人发表2个主题!就是参与人数1,主题数2!因为SubjectCreateUserId是同1人发的
...全文
723 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
TongJL1993 2017-02-07
  • 打赏
  • 举报
回复
引用 2 楼 Chinajiyong 的回复:
select new { ModuleName = grp.Key.ModuleName, SubjectUserCount =grp.Count(), ReplyUserCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyCreateUserId)),//回复人数 ReplyCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyId)),//回复数 SubjectCount = grp.Count(c => !string.IsNullOrEmpty(c.SubjectId))//发表数 };
而且也不行
TongJL1993 2017-02-07
  • 打赏
  • 举报
回复
引用 2 楼 Chinajiyong 的回复:
select new { ModuleName = grp.Key.ModuleName, SubjectUserCount =grp.Count(), ReplyUserCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyCreateUserId)),//回复人数 ReplyCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyId)),//回复数 SubjectCount = grp.Count(c => !string.IsNullOrEmpty(c.SubjectId))//发表数 };
不行!有写没有发不过主题的也统计出来了
EnForGrass 2017-02-07
  • 打赏
  • 举报
回复
select new { ModuleName = grp.Key.ModuleName, SubjectUserCount =grp.Count(), ReplyUserCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyCreateUserId)),//回复人数 ReplyCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyId)),//回复数 SubjectCount = grp.Count(c => !string.IsNullOrEmpty(c.SubjectId))//发表数 };
TongJL1993 2017-02-07
  • 打赏
  • 举报
回复
    var query = from item in
                            (from en in dbContext.WK_TK_Module.Where(c => c.State == (int)SYS_State.启用)
                             join subject in dbContext.WK_TK_Subject.Where(c => c.State == (int)SYS_State.启用) on en.Id equals subject.ModuleId into ts2
                             from ts in ts2.DefaultIfEmpty()
                             //join user in dbContext.SYS_User on ts.CreateUserId equals user.Id into tu2
                             //from tu in tu2.DefaultIfEmpty()
                             join reply in dbContext.WK_TK_Reply.Where(c => c.State == (int)SYS_State.启用) on ts.Id equals reply.SubjectId into tr2
                             from tr in tr2.DefaultIfEmpty()
                             select new
                             {
                                 ModuleId = string.IsNullOrEmpty(en.Id) ? null : en.Id,
                                 ModuleName = en.Name,
                                 //UserId = tr.Id == "" ? "" : tr.Id,
                                 SubjectId = string.IsNullOrEmpty(ts.Id) ? null : ts.Id,
                                 SubjectCreateUserId = string.IsNullOrEmpty(ts.CreateUserId) ? "asd" : ts.CreateUserId,
                                 ReplyCreateUserId = string.IsNullOrEmpty(tr.CreateUserId) ? null : tr.CreateUserId,
                                 ReplyId = string.IsNullOrEmpty(tr.Id) ? null : tr.Id
                             }
                                )
                        group item by new { item.ModuleName,item.SubjectCreateUserId } into grp
                     
                        select new
                        {
                            ModuleName = grp.Key.ModuleName,
                            SubjectUserCount =grp.Count(c=>!string.IsNullOrEmpty(c.SubjectCreateUserId)), 
                                                                 //grp.GroupBy(c=>c.SubjectCreateUserId).Count(),
                                                                 //grp.Select(c => c.SubjectCreateUserId).Distinct().Count(), //主题发表人数
                            ReplyUserCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyCreateUserId)),//回复人数
                            ReplyCount = grp.Count(c => !string.IsNullOrEmpty(c.ReplyId)),//回复数
                            SubjectCount = grp.Count(c => !string.IsNullOrEmpty(c.SubjectId))//发表数
                        };
            //.ToList<dynamic>();
            return query.ToList<dynamic>();

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧