[英]linq query to get count from multiple tables
我有多個表,從中我想從查詢中的特定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();
我想從“REQUIREMENT POSTING”表和另一個表“SUBSCRIBER_RESOURCE_SUBMISSION”表中獲取COUNT,我已嘗試過如上所述但沒有得到正確的結果,獲得多行而不是一行。 請幫助我。
我認為這對你有用......
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.