1. 程式人生 > 實用技巧 >linq左連線

linq左連線

如果右邊有多條

  var query = from t1 in _studentRepository.GetAll()
                        join t2 in _schoolTermRepository.GetAll() on t1.TermId equals t2.Id
                        join t3 in classs on t1.ClassId equals t3.Id
                        join t7 in _dicRepository.GetAll() on t3.GradeId equals t7.Id
                        join t4 
in _personQulitativeScoreRepository.GetAll() on t1.Id equals t4.StudentId join t6 in school on t1.SchoolId equals t6.Id where (string.IsNullOrEmpty(input.StudentName) || t1.Name == input.StudentName) && (!input.TermId.HasValue || t1.TermId == input.TermId)
select new { t1, t2, t3, t4, t7, t6 };

一個學生對應多個t4評分 ,會出現多條

結果為

{
  "result": {
    "totalCount": 5,
    "items": [
      {
        "studentId": "2e4173cb-a937-a5b7-1ec0-39f6a1d4c917",
        "termName": "2020-2021學年第一學期",
        "schoolName": "職業技術培訓學校",
        "gradeName": "二年級",
        "className": "1-班",
        
"studentName": "寄快遞", "level": 0, "totailScore": 30 }, { "studentId": "2e4173cb-a937-a5b7-1ec0-39f6a1d4c917", "termName": "2020-2021學年第一學期", "schoolName": "職業技術培訓學校", "gradeName": "二年級", "className": "1-班", "studentName": "寄快遞", "level": 0, "totailScore": 30 }, { "studentId": "2e4173cb-a937-a5b7-1ec0-39f6a1d4c917", "termName": "2020-2021學年第一學期", "schoolName": "職業技術培訓學校", "gradeName": "二年級", "className": "1-班", "studentName": "寄快遞", "level": 0, "totailScore": 30 }, { "studentId": "a8d83256-429c-9a19-fead-39f6a1d58a38", "termName": "2020-2021學年第一學期", "schoolName": "職業技術培訓學校", "gradeName": "二年級", "className": "1-班", "studentName": "考考", "level": 0, "totailScore": 18 }, { "studentId": "a8d83256-429c-9a19-fead-39f6a1d58a38", "termName": "2020-2021學年第一學期", "schoolName": "職業技術培訓學校", "gradeName": "二年級", "className": "1-班", "studentName": "考考", "level": 0, "totailScore": 18 } ] }, "targetUrl": null, "success": true, "error": null, "unAuthorizedRequest": false, "__abp": true }