Я пытаюсь преобразовать следующий SQL-запрос в LINQ, но получаю разные результаты с обоими,

SQL-запрос:

SELECT  T5.CNTR,  T5.BenefitCode,T5.ApprovedFlag, 
       T5.PaymentFrequencyCode, T5.InstalmentAmt, T5.TotalAmt,  
       T5.CarRego
       FROM  
       dbo.EmployeeBenefit As T5 
       LEFT JOIN dbo.Payee ON T5.PayeeCntr = dbo.Payee.CNTR 
       LEFT JOIN dbo.BankDetails ON dbo.Payee.BankCntr = dbo.BankDetails.BankCntr 
       Left Join dbo.EmployeeCar As T4 on T5.EmployeeCarCntr=T4.Cntr 
       Inner Join dbo.EmployeeEntity As T1 On T5.EmployeeEntityCntr=T1.EmployeeEntityCntr 
       Inner Join dbo.EmployerEntity As T2 On T1.EmployerEntityCntr=T2.EmployerEntityCntr  
       where T5.EmployeeCntr =  117165
        AND ((T5.EndDate is Null) OR (T5.EndDate >= GETDATE()))  

LINQ :

var result = (from employeeBenefit in context.EmployeeBenefit
                      from payee in context.Payee.Where(x => x.Cntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
                      from bankDetails in context.BankDetails.Where(x => x.BankCntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
                      from employeeCar in context.EmployeeCar.Where(x => x.Cntr == payee.BankCntr).DefaultIfEmpty()
                      from employeeEntity in context.EmployeeEntity
                      where employeeEntity.EmployeeEntityCntr == employeeBenefit.EmployeeEntityCntr
                      from employeeEntity1 in context.EmployeeEntity
                      where employeeEntity.EmployerEntityCntr == employeeEntity1.EmployerEntityCntr
                            && employeeBenefit.EmployeeCntr == iEmployeeID
                            && (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
                            && employeeBenefit.EmployeeCntr == 117165
                            && employeeBenefit.CarRego == registration
                      select new
                      {
                          CNTR = employeeBenefit.Cntr,
                          BenefitCode = employeeBenefit.BenefitCode,
                          PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
                          InstalmentAmount = employeeBenefit.InstalmentAmt,
                          TotalAmount = employeeBenefit.TotalAmt,
                          CarRego = employeeBenefit.CarRego,
                          ApprovedFlag = employeeBenefit.ApprovedFlag
                      }).ToList();

Пожалуйста, дайте мне знать, что мне не хватает.

Для данных в моей базе данных запрос SQL возвращает 10 записей. Но LINQ возвращает 2700 записей.

0
Aryan M 10 Окт 2019 в 08:52
В вашем LINQ нет объединений. См. Ссылку здесь
 – 
Peter Smith
10 Окт 2019 в 08:56
Спасибо за ответ. Не могли бы вы помочь преобразовать приведенный выше SQL в LINQ
 – 
Aryan M
10 Окт 2019 в 08:59

2 ответа

Не полный ответ (опаздываю на работу), но:

var result = (from T5 in context.EmployeeBenefit
              join PY in dbo.Payee on T5.PayeeCntr equals PY.CNTR into PY1
              where T5.EmployeeCntr =  117165
              select new {
                         CNTR = T5.Cntr,
                         ...
                         }
                  ).ToList();
0
Peter Smith 10 Окт 2019 в 09:14

Это была проблема несоответствия условий, которую я сделал в LINQ. Приведенный ниже запрос работал нормально. Спасибо, что помогли мне с проблемой.

var result = (from employeeBenefit in context.EmployeeBenefit
                      from payee in context.Payee.Where(x => x.Cntr == employeeBenefit.PayeeCntr).DefaultIfEmpty() 
                      from bankDetails in context.BankDetails.Where(x => x.BankCntr == payee.BankCntr).DefaultIfEmpty()
                      from employeeCar in context.EmployeeCar.Where(x => x.Cntr == employeeBenefit.EmployeeCarCntr).DefaultIfEmpty()
                      join employeeEntity in context.EmployeeEntity
                      on  employeeBenefit.EmployeeEntityCntr equals employeeEntity.EmployeeEntityCntr
                      join employerEntity in context.EmployerEntity
                      on employeeEntity.EmployerEntityCntr equals employerEntity.EmployerEntityCntr
                      where employeeBenefit.EmployeeCntr == 117165 && (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
                      select new
                      {
                          CNTR = employeeBenefit.Cntr,
                          BenefitCode = employeeBenefit.BenefitCode,
                          PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
                          InstalmentAmount = employeeBenefit.InstalmentAmt,
                          TotalAmount = employeeBenefit.TotalAmt,
                          CarRego = employeeBenefit.CarRego,
                          ApprovedFlag = employeeBenefit.ApprovedFlag
                      }).ToList();
0
Aryan M 10 Окт 2019 в 09:40