如何使用Null Check在Linq中执行左联接?

时间:2019-01-15 12:49:04

标签: c# .net linq

我正在尝试对我的4个表进行左连接,SQL中的左连接按要求工作,但是LINQ左连接抛出错误。

  

System.NullReferenceException:'对象引用未设置为对象的实例。'<> f__AnonymousType3 <<<> h__TransparentIdentifier2> j__TPar,j__TPar> .sdadobj.get返回null。   
  在线

join ar in uow.Repository<Models.Models.AdmissionRejection>().GetAll() 
           on sdadobj.ADID equals ar.ADID into adar

下面是我的LINQ

 public List<AdmissionRejectionVM> GetAllDefaultSchoolFee()
    {
        var lsit = (from sd in uow.Repository<Models.Models.StudentDetail>().GetAll().ToList()
                    join bad in uow.Repository<Models.Models.BilledAmountDetail>().GetAll().ToList() on sd.StudentId equals bad.StudentId into sdbad
                    from sdbadobj in sdbad.DefaultIfEmpty()

                    join ad in uow.Repository<Models.Models.AdmissionDetail>().GetAll() on sd.StudentId equals ad.StudentId into sdad
                    from sdadobj in sdad.DefaultIfEmpty()

                    join ar in uow.Repository<Models.Models.AdmissionRejection>().GetAll() on sdadobj.ADID equals ar.ADID into adar
                    from adarobj in adar.DefaultIfEmpty()
                    select new AdmissionRejectionVM
                    {
                        studentid = sd.StudentId,
                        fullname = sd.FirstName + " " + sd.MiddleName + " " + sd.LastName,
                        classname = sdbadobj != null ? "Class-"+sdbadobj.Grade:" --" ,
                        admissionstatus = sdbadobj != null ? true : true,
                        admissiondate = sdadobj != null ? sdadobj.DOA : (DateTime?)null,
                        rejectionreason = adarobj !=  null ? adarobj.ReasonOfRejection : "--"
                    }).ToList();
        return lsit.ToList();
    }


以下是我的SQL

select sd.FirstName,ba.Grade,ad.DOA,ba.AdmissionStatus,ar.ReasonOfRejection 
from StudentDetails sd
left join BilledAmountDetails as ba on sd.StudentId = ba.StudentId
left join AdmissionDetails as ad on sd.StudentId = ad.StudentId
left join AdmissionRejection ar on ad.ADID = ar.ADID 

0 个答案:

没有答案