[英]linq query to get count from multiple tables
I have multiple tables and from that I want to get count from tables on specific ID from the query like below : 我有多个表,从中我想从查询中的特定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();
I Want to get COUNT from "REQUIREMENT POSTING" table and another table "SUBSCRIBER_RESOURCE_SUBMISSION" table, I have tried like above but not getting proper result, getting multiple rows instead of one row. 我想从“REQUIREMENT POSTING”表和另一个表“SUBSCRIBER_RESOURCE_SUBMISSION”表中获取COUNT,我已尝试过如上所述但没有得到正确的结果,获得多行而不是一行。 Please help me anyone. 请帮助我。
I think this will work for you... 我认为这对你有用......
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.