C# LINQ:使用公共工作计数选择工资估算
我有3份薪水预算C# LINQ:使用公共工作计数选择工资估算,c#,linq,C#,Linq,我有3份薪水预算A、B和C 我想输出工资估算(工作数量) 顺便说一句,C不会退出bec,因为它下的作业不是公开的,也不会被删除 下面是我的代码,它可以输出我想要的东西,但是有点长。有更好的办法吗 var publicJobsQuery = _dbContext.Jobs .Where(j => j.IsDeleted.Equals(false)) .Where(j => j.IsPrivate.Equals(false)); var jobsIdHs = new H
A、B和C
我想输出工资估算(工作数量)
顺便说一句,C不会退出bec,因为它下的作业不是公开的,也不会被删除
下面是我的代码,它可以输出我想要的东西,但是有点长。有更好的办法吗
var publicJobsQuery = _dbContext.Jobs
.Where(j => j.IsDeleted.Equals(false))
.Where(j => j.IsPrivate.Equals(false));
var jobsIdHs = new HashSet<int>(publicJobsQuery.Select(j => j.JobId));
var salaryEstimatesWithJobs = _dbContext.SalaryEstimates
.Include(s => s.Jobs)
.Where(s => s.Jobs.Any(j => jobsIdHs.Contains(j.JobId)))
.Select(s => new SalaryEstimatesWithJobCountViewModel
{
SalaryEstimate = s,
JobCount = s.Jobs.Count(j => jobsIdHs.Contains(j.JobId))
});
var publicJobsQuery=\u dbContext.Jobs
其中(j=>j.IsDeleted.Equals(false))
其中(j=>j.IsPrivate.Equals(false));
var jobsIdHs=newhashset(publicJobsQuery.Select(j=>j.JobId));
var salaryEstimatesWithJobs=\u dbContext.SalaryEstimates
.包括(s=>s.Jobs)
.Where(s=>s.Jobs.Any(j=>jobsIdHs.Contains(j.JobId)))
.选择(s=>new SalaryEstimatesWithJobCountViewModel
{
工资估算=s,
JobCount=s.Jobs.Count(j=>jobsIdHs.Contains(j.JobId))
});
好吧,您可以通过作业
开始查询,并通过以下方式应用组:
var salaryEstimatesWithJobs = _dbContext.Jobs
.Include(s => s.SalaryEstimates)
.Where(j => !j.IsDeleted && !j.IsPrivate)
.GroupBy(e=>e.SalaryEstimateId)
.Select(g => new SalaryEstimatesWithJobCountViewModel
{
SalaryEstimate = g.FirstOrDefault().SalaryEstimate,
JobCount = g.Count()
});
var salaryEstimatesWithJobs = _dbContext.Jobs
.Include(s => s.SalaryEstimates)
.Where(j => !j.IsDeleted && !j.IsPrivate)
.GroupBy(e=>e.SalaryEstimateId)
.Select(g => new SalaryEstimatesWithJobCountViewModel
{
SalaryEstimate = g.FirstOrDefault().SalaryEstimate,
JobCount = g.Count()
});