我想使用一个 linq-to-sql 查询,它将使用内连接和左外连接(使用 .DefaultIfEmpty()),但查询不起作用。这让我很烦恼,因为它是 T-SQL 中的一项简单任务。这是我的查询:
var results = from cy in _dc.Companies
join cyv in _dc.CompanyVersions
on cy.CompanyId equals cyv.CompanyId
join cyd in _dc.CompanyDetails
on cyv.CompanyVersionId equals cyd.Id
join cd in _dc.CustomerDetails
on cy.CompanyId equals cd.CompanyId
join d in _dc.Documents
on cd.CustomerId equals d.CustomerId
join di in _dc.DocumentItems
on d.DocumentId equals di.DocumentId
join dd in _dc.DocumentDetails
on di.DocDetailsId equals dd.DocumentDetailsId
join dt in _dc.DetailTypes
on dd.detailTypeId equals dt.detailTypeId
join vt in _dc.Vats
on dt.VATCode equals vt.VATCode
join v in _dc.Vouchers.DefaultIfEmpty()
on dd.DocumentDetailsId equals v.DocDetailID
join cc in _dc.CreditCards.DefaultIfEmpty()
on v.CCType equals cc.CCType
where
(d.DocTypeId == 1 || d.DocTypeId == 2) && (d.DocDate >= date)
&&
(d.DocDate < date.AddDays(1)) &&
(d.CompanyVersionId == cyv.CompanyVersionId)
&& (d.isDeleted == false || d.isDeleted == null)
orderby d.DocNumber ascending
select new
{
d.DocumentId,
d.DocNumber,
d.AutUser,
d.GrossAmount,
d.DocTypeId,
cyd.Name,
cyd.AddressLine1,
cyd.AddressLine2,
cyd.TelephoneLine,
cyd.FaxLine,
v.FromVoucNbr,
d.DocDate,
cd.CustomerName,
cd.StreetAddress,
cd.City,
cd.Country,
dt.DetDescr,
cc.CCType,
dd.FreeDescr,
dd.NetAmount,
dd.VATAmount,
vt.VATDescr,
cc.Description,
vt.VATRate
};
谁能建议我如何在此查询的最后 2 个表上使用左外连接?