如何在linq中编写两个左连接

时间:2017-05-24 05:31:23

标签: c# sql-server linq

   var CCEScholasticTests = db.CCEScholasticTests.Where(x => x.CCEvaluationID == CCEValuationID && x.SubjectID == SubjectID && x.ClassID == ClassID && (x.BranchSectionID == BranchSectionID || BranchSectionID == 0) && x.languageTypeSubjectID == languageTypeSubjectID && (x.BranchID == BranchID || x.BranchID == 0) && x.IsOnlyGrade == false).Select(x => x).ToList();
var res = (from a in CCEScholasticTests
                               join b in db2.CCESubjectSkills on new { key1 = a.CCESubjectSkillID } equals new { key1 = b.CCESubjectSkillID } into join1
                               from joinRes in join1.DefaultIfEmpty(new CCESubjectSkill())
                               join c in db2.CCEScholasticSkillsMasters on new { key = joinRes.CCEScholasticSkillMasterID } equals new { key = c.CCEScholasticSkillMasterID } into join2
                               from joinRes2 in join2.DefaultIfEmpty(new CCEScholasticSkillsMaster())
                               select new EvaluationBluk
                               {
                                   BranchSectionID = a.BranchSectionID,
                                   CCEScholasticTestID = a.CCEScholasticTestID,
                                   CCEScholasticTestName = a.CCEScholasticTestName,
                                   //CCESubjectSkillName = joinRes.CCESubjectSkillName,
                                   CCESubjectSkillID = a.CCESubjectSkillID,
                                   CCEvaluationID = a.CCEvaluationID,
                                   MaxMarks = a.MaxMarks,
                                   SubjectID = a.SubjectID,
                                   TestDate = a.TestDate,
                                   MarksEntryLastDate = a.MarksEntryLastDate,
                                   //CCEScholasticSkillMasterID = joinRes.CCEScholasticSkillMasterID,
                                   //CCEScholasticSkillName = joinRes2.CCEScholasticSkillName
                               }).ToList();

如何在linq中编写多个左连接..下面的linq查询我正在写这个sp

CREATE procedure [dbo].[GetClassCCEScholasticTestsOnlyMarksTest]                
(             
@BranchSectionID int,                        
@CCEvaluationID int,                      
@SubjectID int,                    
@ClassID int ,                
@languageTypeSubjectID int ,          
@BranchID int                     
)                        
as                        
select c1.BranchSectionID,c1.CCEScholasticTestID,c1.CCEScholasticTestName,s1.CCESubjectSkillName,                        
c1.CCESubjectSkillID,c1.CCEvaluationID,c1.MaxMarks,c1.SubjectID,convert(varchar,c1.TestDate,101) as TestDate,convert(date,c1.MarksEntryLastDate,101)as MarksEntryLastDate                        
,isnull(s1.CCEScholasticSkillMasterID,-1) as CCEScholasticSkillMasterID ,cm.CCEScholasticSkillName     

from dbo.CCEScholasticTests c1                          
left join CCESubjectSkills s1 on s1.CCESubjectSkillID=c1.CCESubjectSkillID      
left join CCEScholasticSkillsMaster cm on cm.CCEScholasticSkillMasterID=s1.CCEScholasticSkillMasterID                      
 where  c1.CCEvaluationID=@CCEvaluationID  and c1.SubjectID=@SubjectID                      
 and c1.ClassID=@ClassID and (c1.BranchSectionID=@BranchSectionID or c1.BranchSectionID=0) and c1.languageTypeSubjectID =@languageTypeSubjectID             
 and (c1.BranchID=@BranchID or c1.BranchID=0)and c1.IsOnlyGrade=0        
 order by c1.CCEScholasticTestID asc

在sql中抛出3行但是在linq中它只抛出1行...我的代码中有什么错误

1 个答案:

答案 0 :(得分:0)

    var result=CCEScholasticTests.join(db2.CCESubjectSkills ,o=>o.CCESubjectSkillID, od=>od.CCESubjectSkillID,(o, od)=> new 
    {
                       BranchSectionID = o.BranchSectionID,
                                       CCEScholasticTestID = o.CCEScholasticTestID,
                                       CCEScholasticTestName = o.CCEScholasticTestName,
                                       CCESubjectSkillID = o.CCESubjectSkillID,
                                       CCEvaluationID = o.CCEvaluationID,
                                       MaxMarks = o.MaxMarks,
                                       SubjectID = o.SubjectID,
                                       TestDate = o.TestDate,
                                       MarksEntryLastDate = o.MarksEntryLastDate,
CCEScholasticSkillMasterID = od.CCEScholasticSkillMasterID
    }).join(db2.CCEScholasticSkillsMasters, s=>s.CCEScholasticSkillMasterID = t.CCEScholasticSkillMasterID, t=>t.CCEScholasticSkillMasterID,(s,t)=> new
    {
                       BranchSectionID = o.BranchSectionID,
                                       CCEScholasticTestID = o.CCEScholasticTestID,
                                       CCEScholasticTestName = o.CCEScholasticTestName,
                                       CCESubjectSkillID = o.CCESubjectSkillID,
                                       CCEvaluationID = o.CCEvaluationID,
                                       MaxMarks = o.MaxMarks,
                                       SubjectID = o.SubjectID,
                                       TestDate = o.TestDate,
                                       MarksEntryLastDate = o.MarksEntryLastDate,
                       CCESubjectSkillName = t.CCESubjectSkillName,
                       CCEScholasticSkillMasterID = t.CCEScholasticSkillMasterID,
                                       CCEScholasticSkillName = t.CCEScholasticSkillName
    }).tolist();

我不知道表之间的确切关系但是,我已经假设和书面查询。希望它有助于你