简体   繁体   English

在LINQ查询中执行内部联接

[英]Perform inner join in LINQ query

I need join in LINQ query, I have done individually but struggling to do in one process. 我需要加入LINQ查询,我已经单独完成但在一个过程中很难做到。

SQL Script SQL脚本

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 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. 首先,您应该理解,当您调用ToList()EF向数据库发出请求时,我的意思是现在您对db进行了3个独立的请求,而不是一个。 Call ToList only when you ready to execute one main query. 仅在准备执行一个主查询时才调用ToList。

And about join Linq has join operator just use it You can use sql like syntax or expression syntax see this question for examples 关于join Linq只需使用join运算符就可以了。您可以使用sql之类的语法或表达式语法, 请参见此问题的示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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