简体   繁体   中英

Perform inner join in LINQ query

I need join in LINQ query, I have done individually but struggling to do in one process.

SQL Script

SELECT af.submission_id, af.created_date, af.surname,af.first_name
FROM app_forms af
INNER JOIN (SELECT * FROM sync_audit_log sal WHERE sal.log_Status='EP' AND sal.lookup_id IS NULL AND id=(SELECT Max(id) FROM sync_audit_log sal2 WHERE  sal.submission_id=sal2.submission_id)) sal ON sal.submission_id=af.submission_id 
LEFT JOIN ebs_sync es ON af.submission_id=es.submission_id
WHERE es.person_code IS NULL

LINQ

 var query = (from af in _uof.Web_AppFormsRepository.GetAll()
                         select af).ToList();

 var query2 = (from sal in _uof.Web_SyncAuditLogRepository.GetAll().Where(sal => sal.LOG_STATUS.Equals("EP") && sal.LOOKUP_ID!=null )
                          select sal.ID).ToList();

 var query3 = (from sal2 in _uof.Web_SyncAuditLogRepository.GetAll()
                          select new { sal2.ID }).ToList();

First of all you should understand that when you call ToList() EF do request to Database, I mean now you do 3 independent requests to db instead of only one. Call ToList only when you ready to execute one main query.

And about join Linq has join operator just use it You can use sql like syntax or expression syntax see this question for examples

IEnumerable<T> leftInnerJoin = from i in left join j in right on i.condition equals j.condition select i;

IEnumerable<T> leftOuterJoin = from i in left join j in right on i.condition equals j.condition into grp from k in grp.DefaultIfEmpty() where k == null select i;

IEnumerable<T> rightOuterJoin = from i in right join j in left on i.condition equals j.condition into grp from k in grp.DefaultIfEmpty() where k == null select i;

IEnumerable<T> groupJoin = leftOuterJoin.Union(rightOuterJoin).Union(leftInnerJoin);

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