我正在尝试在 Linq 中重现以下 SQL 查询,需要一些帮助。
select
dbo.Documents.DocId,
dbo.Documents.ReykerAccountRef,
dbo.Documents.ReykerClientId DocClientID,
CAAs.ClientId CAAClientIDCheck,
ClientData.FullName ClientFullName,
CAAs.IFAId,
AdvisorData.FullName AdvisorFullName
from dbo.Documents
left join
dbo.CAAs on dbo.Documents.ReykerAccountRef = dbo.CAAs.AccountRef
left join
dbo.hmsProfileDatas AS ClientData
on
dbo.CAAs.ClientId = ClientData.ReykerClientID
left join
dbo.hmsProfileDatas AS AdvisorData
on
dbo.CAAs.IFAId = AdvisorData.ReykerClientID
我试图链接到同一个表两次,一次用于客户全名,另一次用于顾问全名。
我想在 linq 中生成的基本 sql 是
select table1.*,table2.*,a.Fullname, b.Fullname
from table1
left join
table2 on table1.t2Id = table2.Id
left join
table3 AS a
on
table2.t3Id1 = table3.id1
left join
table3 AS b
on
table2.t3Id2 = table3.id2
所以表一连接到表 2,表 2 有 2 个外键(t3Id1 和 t3Id2)到表 3 到不同的字段(id1 和 id2)。
这是我按照一些指导尝试过的,但它没有返回任何东西!怎么了?
var results3 = from doc in DataContext.Documents
from caa
in DataContext.CAAs
.Where(c => c.AccountRef == doc.ReykerAccountRef)
.DefaultIfEmpty()
from cpd
in DataContext.hmsProfileDatas
.Where(pdc => pdc.ReykerClientID == caa.ClientId)
.DefaultIfEmpty()
from apd
in DataContext.hmsProfileDatas
.Where(pda => pda.ReykerClientID == caa.IFAId)
.DefaultIfEmpty()
select new DocumentInList()
{
DocId = doc.DocId,
DocTitle = doc.DocTitle,
ReykerDocumentRef = doc.ReykerDocumentRef,
ReykerAccountRef = doc.ReykerAccountRef,
ClientFullName = cpd.FullName,
AdvisorFullName = apd.FullName,
DocTypeId = doc.DocTypeId,
DocTypes = doc.DocTypes,
DocDate = doc.DocDate,
BlobDocName = doc.BlobDocName,
UploadDate = doc.UploadDate,
};