我有多个表,我希望从查询中的特定ID的表中获取计数,如下所示:
var jobsPostList = (from jobposting in reslandEntities.JOB_POSTING
join skill in reslandEntities.SKILL on jobposting.SKILL_ID equals skill.ID
from req in reslandEntities.REQUIREMENT_POSTING
where jobposting.POSTED_BY_ID == RES_ID && jobposting.POSTED_BY == "CLIENT"
&& jobposting.IS_DELETED == "N"
&& jobposting.STAT!="DRAFT"
&& req.JOB_ID==jobposting.ID
select new PostJobModel
{
ID = jobposting.ID,
JOB_ID = jobposting.JOB_ID,
POST_TITLE = jobposting.TITLE,
POST_DT = jobposting.POST_DT,
POST_END_DT = jobposting.POST_END_DT,
TECH_SKILLS = skill.TECH_SKILLS,
POSITIONS_CNTS = jobposting.POSITIONS_CNT,
DURATION = jobposting.DURATION,
LOCATION = jobposting.LOCATION,
STAT = jobposting.STAT,
DT_CR = jobposting.DT_CR,
SUB_COMP_COUNT = reslandEntities.REQUIREMENT_POSTING.Where(m => m.JOB_ID == jobposting.ID).Select(m => m.COMP_ID).Count(),
COMP_COUNT = companyCount,
CAND_COUNT = reslandEntities.SUBSCRIBER_RESOURCE_SUBMISSION.Where(m=>m.CLIENT_JOB_POSTINGID==req.ID).Select(m=>m.RES_ID).Count()
}).Distinct().ToList();
我希望从"要求发布"表和另一个表" SUBSCRIBER_RESOURCE_SUBMISSION"表,我已经尝试过但没有得到正确的结果,得到多行而不是一行。请帮助我。
答案 0 :(得分:3)
我认为这对你有用......
var jobsPostList = (from jobposting in reslandEntities.JOB_POSTING
join skill in reslandEntities.SKILL on jobposting.SKILL_ID equals skill.ID
join grpComp in
(
from jobsent in reslandEntities.REQUIREMENT_POSTING
group jobsent by jobsent.JOB_ID into grp
select new { grp.Key, count = grp.Select(m => m.COMP_ID).Count() }
) on jobposting.ID equals grpComp.Key
let result = (
from sub in reslandEntities.SUBSCRIBER_RESOURCE_SUBMISSION
join req in reslandEntities.REQUIREMENT_POSTING on sub.CLIENT_JOB_POSTINGID equals req.ID
where req.JOB_ID == jobposting.ID
select sub.RES_ID
)
where jobposting.POSTED_BY_ID == RES_ID && jobposting.POSTED_BY == "CLIENT"
&& jobposting.IS_DELETED == "N"
&& jobposting.STAT!="DRAFT"
select new PostJobModel
{
ID = jobposting.ID,
JOB_ID = jobposting.JOB_ID,
POST_TITLE = jobposting.TITLE,
POST_DT = jobposting.POST_DT,
POST_END_DT = jobposting.POST_END_DT,
TECH_SKILLS = skill.TECH_SKILLS,
POSITIONS_CNTS = jobposting.POSITIONS_CNT,
DURATION = jobposting.DURATION,
LOCATION = jobposting.LOCATION,
STAT = jobposting.STAT,
DT_CR = jobposting.DT_CR,
SUB_COMP_COUNT = grpComp.count,
COMP_COUNT = companyCount,
CAND_COUNT = result.Count()
}).ToList();