简体   繁体   中英

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 :

         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. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM