0

我想使用一个 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 个表上使用左外连接?

4

2 回答 2

1

你把两个都放.DefaultIfEmpty错了。我认为它是这样的(尽管我不确定 where 子句..)。

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 on dd.DocumentDetailsId equals v.DocDetailID into other1
  from o1 in other1.DefaultIfEmpty()
  join cc in _dc.CreditCards on v.CCType equals cc.CCType into other2
  from o2 in other2.DefautlIfEmtpy() 
  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,
                o1.FromVoucNbr,
                d.DocDate,
                cd.CustomerName,
                cd.StreetAddress,
                cd.City,
                cd.Country,
                dt.DetDescr,
                o2.CCType,
                dd.FreeDescr,
                dd.NetAmount,
                dd.VATAmount,
                vt.VATDescr,
                o2.Description,
                vt.VATRate
            };
于 2013-10-03T17:43:09.103 回答
0

是的,连接的顺序不正确,我现在已经修复了这里是工作和编译的查询:

ps 谢谢大家的帮助!

  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 on dd.DocumentDetailsId equals v.DocDetailID into 
            other1
            from o1 in other1.DefaultIfEmpty()
            join cc in _dc.CreditCards on o1.CCType equals cc.CCType into other2
            from o2 in other2.DefaultIfEmpty()
            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,
                           o1.FromVoucNbr,
                           d.DocDate,
                           cd.CustomerName,
                           cd.StreetAddress,
                           cd.City,
                           cd.Country,
                           dt.DetDescr,
                           o2.CCType,
                           dd.FreeDescr,
                           dd.NetAmount,
                           dd.VATAmount,
                           vt.VATDescr,
                           o2.Description,
                           vt.VATRate
                       };
于 2013-10-03T20:51:00.237 回答