使用Linq-To-Sql左外连接

时间:2013-10-03 17:03:26

标签: linq-to-sql

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

2 个答案:

答案 0 :(得分: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
            };

答案 1 :(得分:0)

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

P.S。谢谢大家的帮助!

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