实体框架核心 - (子查询的种类)

时间:2017-07-31 20:36:00

标签: c# entity-framework linq entity

我使用流畅的API和Ef Core。

这是我的linq:

 var data = (from candidateHP in _cempContexto.CandidateHiringProcessSummary
              join jobsHP in _cempContexto.JobHiringProcessSummary on candidateHP.JobCode equals jobsHP.JobCode
              join job in _cempContexto.Vaga on jobsHP.JobCode equals job.Codigo
              join candidateJob in _cempContexto.TrCandidatoVaga on new { X = job.Codigo, Y = candidateCode } equals new { X = candidateJob.VagaCodigo, Y = candidateJob.CandidatoCodigo }
              where
              candidateHP.CandidateCode == candidateCode
              select
              new
              {
                JobCode = jobsHP.JobCode,
                CompanyCode = job.EmpresaCodigo,
                Title = job.Titulo,
                HasImage = true,
                CompanyName = job.NomeEmpresa,
                QuantityJobs = job.QuantidadeVaga,
                Location = job.CidadeCodigo,
                QuantityResumeSent = jobsHP.QuantityResumeSent,
                JobStatusCode = jobsHP.StatusCode,
                CandidateStatusCode = candidateHP.StatusCode,
                StatusCode = job.StatusCodigo,
                ResumeSentDate = candidateJob.Data,
                InsertDate = job.DataDeCadastro,
                EndDate = job.DataDeSaida,
                city= _cempContexto.TrVagaCidade.SelectMany(p => _cempContexto.TrVagaCidade.Where(q => q.VagaCodigo == candidateHP.JobCode).Select(q => new { q })).ToList()
              })
              .ToList();

我需要用该子查询填充城市。我需要一个更好的方法来做到这一点,我该怎么办?

1 个答案:

答案 0 :(得分:0)

将代码拆分为两个查询:

var query = (from candidateHP in _cempContexto.CandidateHiringProcessSummary
              join jobsHP in _cempContexto.JobHiringProcessSummary on candidateHP.JobCode equals jobsHP.JobCode
              join job in _cempContexto.Vaga on jobsHP.JobCode equals job.Codigo
              join candidateJob in _cempContexto.TrCandidatoVaga on new { X = job.Codigo, Y = candidateCode } equals new { X = candidateJob.VagaCodigo, Y = candidateJob.CandidatoCodigo }
              where
              candidateHP.CandidateCode == candidateCode
              select
              new
              { jobsHP = jobsHP, job = job, candidateJob = candidateJob}).ToList();

var data = query.Select(x => new {
                JobCode = x.jobsHP.JobCode,
                CompanyCode = x.job.EmpresaCodigo,
                Title = x.job.Titulo,
                HasImage = true,
                CompanyName = x.job.NomeEmpresa,
                QuantityJobs = x.job.QuantidadeVaga,
                Location = x.job.CidadeCodigo,
                QuantityResumeSent = x.jobsHP.QuantityResumeSent,
                JobStatusCode = x.jobsHP.StatusCode,
                CandidateStatusCode = x.candidateHP.StatusCode,
                StatusCode = x.job.StatusCodigo,
                ResumeSentDate = x.candidateJob.Data,
                InsertDate = x.job.DataDeCadastro,
                EndDate = x.job.DataDeSaida,
                city= query.Select(y => y.candidateHP.JobCode).Select(q => new { q })).ToList()
              })
              .ToList();