EFCore中Linq與Lambda的左連接查詢


有兩張表:

專業表:

 

學生表:

 

 

 學生表中外鍵約束mno是專業表的主鍵

EfCore中有兩種連接查詢:

Lambda表達式的Join:

 public async Task<dynamic> GetStudentDataLambdaAsync()
        {
            //Join<TOuter, TInner, TKey, TResult>
            var res = _appDbContext.Major.Join(_appDbContext.Student, a => a.mno, g => g.mno, (a, g) => new
            {
                a.mno,
                a.manme,
                g.sno,
                sex = g.sex == true ? "" : "",
                g.sname
            });
            return res;
        }

Linq的Join:

注意引用:using System.Linq;

public async Task<dynamic> GetStudentDataLinqAsync()
        {
            var res = from m in _appDbContext.Major
                      join s in _appDbContext.Student on m.mno equals s.mno into r
                      from a in r.DefaultIfEmpty()
                      select new
                      {
                          a.sno,
                          a.sname,
                          sex = a.sex == true ? "" : "",
                          a.mno,
                          m.manme,
                      };
            return res;
        }

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM