1

I have question which SQL to lambda expression with LEFT OUTER JOIN in subquery. SQL return 147 values, and Lambda Expression return 135 values. They retrieving different amoutn of records. So, I tried find out to lambda expression with left outher join with subquery, but there is no result with lambda expression which I want. So, Please help me to find out what I`m missing.

This is my SQL query :

select  
c.CompanySeq,
c.CompanyName,
c.CompanyInitial,
c.PartnerName,
c.Domain,
c.CompanyStatus,
c.ServiceStartDate,
c.RequestStatus,
c.ConsultantingRequest,
c.IsFixedPrice,
c.CMSStatus,
ISNULL(E.UnPaidCnt, 0) AS UnPaidCnt
from _TErpCustomer as c
LEFT JOIN (
    SELECT CompanySeq, COUNT(PaymentStatus) UnPaidCnt   
    FROM _TBillingInfo               
    WHERE PaymentStatus = 0              
    GROUP BY CompanySeq) as E on c.CompanySeq = E.CompanySeq

And this is my C# lambda expression Code.

var test2 = (from c in repository.Customers
                     join t in
                              from b in repository.Billings
                              where b.PaymentStatus == false
                              group b by new { b.PaymentStatus, b.CompanySeq } into g
                              select new
                              {
                                  companyseq = g.Key.CompanySeq,
                                  cnt = g.Count()
                              } on c.CompanySeq equals t.companyseq
                     where c.CompanyStatus == searchStatus
                     select new CustomerListModel
                     {
                         CompanySeq = c.CompanySeq,
                         CompanyName = c.CompanyName,
                         CompanyInitial = c.CompanyInitial,
                         PartnerName = c.PartnerName,
                         Domain = c.Domain,
                         CompanyStatus = c.CompanyStatus,
                         ServiceStartDate = c.ServiceStartDate,
                         RequestStatus = c.RequestStatus,
                         ConsultantingRequest = (bool)c.ConsultantingRequest,
                         IsFixedPrice = c.IsFixedPrice,
                         CMSStatus = c.CMSStatus,
                         UnPaidCnt = t.cnt
                     }).OrderByDescending(m => m.ServiceStartDate).ThenBy(m => m.CompanyName).ToList();

Please any solution or answer.


I`ve solved this problem. this is refer to left outer join in lambda/method syntax in Linq, and LINQ query with sub-query on LEFT JOIN conditions

MY SOLUTION :

model.List = (from c in repository.Customers
                      join t in
                               from b in repository.Billings
                               where b.PaymentStatus == false
                               group b by new { b.PaymentStatus, b.CompanySeq } into g
                               select new
                               {
                                   companyseq = g.Key.CompanySeq,
                                   cnt = g.Count()
                               } on c.CompanySeq equals t.companyseq into x
                      from t in x.DefaultIfEmpty()  // LEFT OUTER JOIN
                      select new CustomerListModel
                      {
                          CompanySeq = c.CompanySeq,
                          CompanyName = c.CompanyName,
                          CompanyInitial = c.CompanyInitial,
                          PartnerName = c.PartnerName,
                          Domain = c.Domain,
                          CompanyStatus = c.CompanyStatus,
                          ServiceStartDate = c.ServiceStartDate,
                          RequestStatus = c.RequestStatus,
                          ConsultantingRequest = (bool)c.ConsultantingRequest,
                          IsFixedPrice = c.IsFixedPrice,
                          CMSStatus = c.CMSStatus,
                          UnPaidCnt = t == null ? 0 : t.cnt
                      }).Where(m => m.CompanyStatus.Equals(searchStatus)).OrderByDescending(m => m.ServiceStartDate).ThenBy(m => m.CompanyName).AsQueryable();
Woojun.Kim
  • 11
  • 2
  • For things like this I always find it handy to turn the c# into sql by getting ORM to log what sql it generates, then I can run the sql repeatedly and find out where it is wrong, then fix the c# – Caius Jard Apr 16 '21 at 04:38

0 Answers0