【EF】多表查询,左联接

这是内联接

DataModelContainer db = new DataModelContainer();
var dataModels = from slide in db.Slide
                 join slideBarcode in db.SlideBarcode on slide.ID equals slideBarcode.SlideID
                 where slide.IsDeleted == false && slide.CaseID == caseId
                 orderby slide.ID
                 select new Slide
                 {
                     SlideID = slide.ID,
                     Description = slide.Description,
                     CreatedDate = slide.CreatedDate,

                     Barcode = slideBarcode .Barcode,
                     PrintDate = slideBarcode .PrintDate
                 };

这是左联接

DataModelContainer db = new DataModelContainer();
var dataModels = from slide in db.Slide
                 join slideBarcode in db.SlideBarcode on slide.ID equals slideBarcode.SlideID into slidebarcode
                 from sb in slidebarcode.DefaultIfEmpty()
                 where slide.IsDeleted == false && slide.CaseID == caseId
                 orderby slide.ID
                 select new Slide
                 {
                     SlideID = slide.ID,
                     Description = slide.Description,
                     CreatedDate = slide.CreatedDate,

                     Barcode = sb.Barcode,
                     PrintDate = sb.PrintDate
                 };

使用into和DefaultIfEmpty()方法

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页