如何根据linq中的日期月份计算总和 [英] how to get the sum count based on the month of a date in linq

查看:92
本文介绍了如何根据linq中的日期月份计算总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i确实要求根据每月的记录数来得到总和结果。

i编写的代码如下。 />


  var  Interviews = spe.Interviews 
.Where(w = > w.InterviewDate.Year == year)
.Join(spe.Recruiters,i = > ; i.Recruiter,r = > r.RecruiterId,(i,r)= > new
{
r.RecruiterId,
i.InterviewDate
})。GroupBy(gp = < span class =code-keyword>> new {RecruiterId = gp.RecruiterId,InterviewDate = gp.InterviewDate})
。选择(x = > new { RecruiterId = x.Key.RecruiterId,InterviewDate = x.Key.InterviewDate,InterviewCount = x.Count()})。ToList();





注意: - 在上面的代码中,我得到了计数,即面试计数日期,但我想要的计数是每个月,请帮我实现这个!!!

解决方案

  var  Interviews = spe.Interviews 
.Where(w = > w.InterviewDate.Year == year)
.Join(spe.Recruiters,i = > ; i.Recruiter,r = > r.RecruiterId,(i,r)= > new
{
r.RecruiterId,
i.InterviewDate
})。GroupBy(gp = < span class =code-keyword>> new {Recruite rId = gp.RecruiterId,Month = gp.InterviewDate.Month})
.Select(x = > new {RecruiterId = x.Key.RecruiterId,InterviewMonth = x.Key.Month,InterviewCount = x.Count()})。ToList();





如果你需要将月份换成字符串替换

 Month = gp.InterviewDate.Month 

with

月= gp.InterviewDate.ToString(  MMM 


hi all,
i do have a requirement that to get the sum result based on the no of records per month.
i have written the code as following.

var Interviews = spe.Interviews
                .Where(w => w.InterviewDate.Year == year)
                .Join(spe.Recruiters, i => i.Recruiter, r => r.RecruiterId, (i, r) => new
                {
                    r.RecruiterId,
                    i.InterviewDate
                }).GroupBy(gp => new { RecruiterId = gp.RecruiterId, InterviewDate = gp.InterviewDate })
                .Select(x => new { RecruiterId = x.Key.RecruiterId, InterviewDate = x.Key.InterviewDate, InterviewCount = x.Count() }).ToList();



Note:- In the above code i am getting the count i.e interview count by date , but the count which i want is per month, please help me to achieve this!!!

解决方案

var Interviews = spe.Interviews
                .Where(w => w.InterviewDate.Year == year)
                .Join(spe.Recruiters, i => i.Recruiter, r => r.RecruiterId, (i, r) => new
                {
                    r.RecruiterId,
                    i.InterviewDate
                }).GroupBy(gp => new { RecruiterId = gp.RecruiterId, Month= gp.InterviewDate.Month })
                .Select(x => new { RecruiterId = x.Key.RecruiterId, InterviewMonth = x.Key.Month, InterviewCount = x.Count() }).ToList();



if you need to get month as string replace

Month= gp.InterviewDate.Month

with

Month= gp.InterviewDate.ToString("MMM")


这篇关于如何根据linq中的日期月份计算总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆