SQL в LINQ с несколькими объединениями и группированием по - PullRequest
0 голосов
/ 13 сентября 2018

Я работаю над преобразованием приведенного ниже кода SQL в запрос LINQ для MVC.Он получил несколько вложенных объединений и групп.

  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 голосов
/ 13 сентября 2018

Я смоделировал ваш запрос с классами, чтобы получить правильный синтаксис:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            List<WorkingInterestGroups> workingInterestGroups = new List<WorkingInterestGroups>();
            List<UnitTracts> unitTracts = new List<UnitTracts>();
            List<Tracts> tracts = new List<Tracts>();
            List<Leases> leases = new List<Leases>();
            List<AdditionalLeaseInfo> additionalLeaseInfos = new List<AdditionalLeaseInfo>();
            List<Interests> interests = new List<Interests>();

            var results = (from unitTract in unitTracts
                join  tract in tracts on unitTract.TractId equals tract.Id 
                join interest in interests on tract.Id equals interest.TractId
                join workingInterestGroup in workingInterestGroups on interest.WorkingInterestGroupId equals workingInterestGroup.Id
                join lease in leases on workingInterestGroup.LeaseId equals lease.Id
                join additionalLeaseInfo in additionalLeaseInfos on lease.Id equals additionalLeaseInfo.LeaseId
                where unitTract.UnitId == "21"
                select new { unitTract = unitTract, tract = tract, interest = interest,  workingInterestGroup = workingInterestGroup,
                    lease = lease, additionalLeaseInfo = additionalLeaseInfo}).ToList();

            var groups = results.GroupBy(x => new
            {
                x.unitTract.Id,
                x.unitTract.UnitId,
                x.lease.Lessor,
                x.lease.Lessee,
                x.lease.Alias,
                x.lease.LeaseDate,
                x.lease.GrossAcres,
                x.lease.Legal,
                x.interest.TractId,
                x.lease.NetAcres,
                x.unitTract.AcInUnit
            })
                .ToList();
        }
    }
    public class WorkingInterestGroups
    {
        public string Id { get; set; }
        public string LeaseId { get; set; }
    }
    public class UnitTracts
    {
        public string TractId { get; set; }
        public string Id { get; set; }
        public string UnitId { get; set; }
        public string AcInUnit { get;set;}
    }
    public class Tracts
    {
        public string Id { get; set; }
    }
    public class Leases
    {
        public string Id { get; set; }
        public string Lessor { get; set; }
        public string Lessee { get; set; }
        public string Alias { get; set; }
        public string LeaseDate { get; set; }
        public string GrossAcres { get; set; }
        public string Legal { get; set; }
        public string NetAcres { get; set; }
    }


    public class AdditionalLeaseInfo
    {
        public string LeaseId { get; set;}
    }
    public class Interests
    {
        public string TractId { get; set; }
        public string WorkingInterestGroupId { get; set; }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...