具有多个联接和分组依据的SQL to LINQ

时间:2018-09-13 17:41:01

标签: c# sql sql-server oracle linq

我正在努力将下面的SQL代码转换为MVC的LINQ查询。它具有多个嵌套联接和分组依据。

  SELECT UnitTracts.Id,  
    UnitTracts.UnitId,  
    Leases.Id,  
    Leases.Lessor,  
    Leases.Lessee,  
    Leases.Alias,  
    Leases.LeaseDate,  
    Leases.GrossAcres,  
    IIf([Page] Is Null,[VolumeDocumentNumber],[VolumeDocumentNumber] + '/' + [Page]) AS [Vol/Pg], 
    Leases.Legal,  
    Interests.TractId,  
    Leases.NetAcres,  
    UnitTracts.AcInUnit 

    FROM (UnitTracts INNER JOIN (((WorkingInterestGroups INNER JOIN Interests ON WorkingInterestGroups.Id = Interests.WorkingInterestGroupId)  
    INNER JOIN Tracts ON Interests.TractId = Tracts.Id)  
    INNER JOIN Leases ON WorkingInterestGroups.LeaseId = Leases.Id)  
    ON UnitTracts.TractId = Tracts.Id)  
    LEFT JOIN AdditionalLeaseInfo ON Leases.Id = AdditionalLeaseInfo.LeaseId  

    where unitId = 21 

    GROUP BY UnitTracts.Id,  
    UnitTracts.UnitId,  
    Leases.Id,  
    Leases.Lessor,  
    Leases.Lessee,  
    Leases.Alias,  
    Leases.LeaseDate,  
    Leases.GrossAcres,  
    IIf([Page] Is Null,[VolumeDocumentNumber],[VolumeDocumentNumber] + '/' + [Page]), 
    Leases.Legal,  
    Interests.TractId, 
     Leases.NetAcres,  
    UnitTracts.AcInUnit 

这是我得到的查询,但返回的记录较少。我试图从SQL转换为LINQ,但是没有用。我现在真的很困。

 var leases = (from l in db.Leases
                          where l.Active
                          join ali in db.AdditionalLeaseInfoes on l.Id equals ali.LeaseId
                          where ali.Active
                          join wig in db.WorkingInterestGroups on l.Id equals wig.LeaseId
                          where wig.Active
                          join interest in db.Interests on wig.Id equals interest.WorkingInterestGroupId
                          where interest.Active
                          join tr in db.Tracts on interest.TractId equals tr.Id
                          where tr.Active
                          join ut in db.UnitTracts on tr.Id equals ut.TractId
                          where ut.Active
                          group new { l, wig, interest, tr, ali, ut } by
                          new
                          {
                              Id = ut.Id,
                              UnitId = ut.UnitId,
                              LeaseId = l.Id,
                              Lessor = l.Lessor,
                              Lessee = l.Lessee,
                              Alias = l.Alias,
                              LeaseDate = l.LeaseDate,
                              GrossAcres = l.GrossAcres,
                              VolPg = l.Page == null ? l.VolumeDocumentNumber : l.VolumeDocumentNumber + "/" + l.Page,
                              Legal = l.Legal,
                              TractId = interest.TractId,
                              NetAcres = l.NetAcres,
                              AcInUnit = ut.AcInUnit                             
                          } into lease
                          select new LeasesViewModel
                          {
                              UnitId = lease.Key.UnitId,
                              TractId = lease.Key.TractId,
                              LeaseId = lease.Key.LeaseId,
                              LeaseAlias = lease.Key.Alias,
                              Pooling = lease.Where(x => x.l.Id == lease.Key.LeaseId).Select(x => x.l.NoPooling).FirstOrDefault() ? "No" :
                                        lease.Where(x => x.l.Id == lease.Key.LeaseId).Select(x => x.l.Pooling).FirstOrDefault() ? "Yes" : "No Review",
                              Lessor = lease.Key.Lessor,
                              GrossAc = lease.Key.GrossAcres

                          }).Where(x => x.UnitId == unitId).OrderBy(x => x.TractId).ToList();

感谢帮助!! 感谢帮助!! 感谢帮助!! 感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我使用类为您的查询建模,以使语法正确:

turtle.right()