LINQ學習筆記(5) Join

莊創偉
2 min readNov 13, 2017

建立兩個類別,存放資料如下

//類SQL表示法var x=from o in SLISTfrom p in ScoreList where o.ID == p.StudentID  orderby o.Classroom,o.ID,p.Class,p.score descendingselect new {o.ID,o.Classroom,o.Name,p.Class,p.score};//所需的欄位 列舉

與T-SQL語法的差異性不大,where 的==需注意一下,也可使用

//類SQL表示法 2var x2 = from o in SLISTjoin p in ScoreListon o.ID equals p.StudentIDorderby o.Classroom, o.ID, p.Class, p.score descendingselect new { o.ID, o.Classroom, o.Name, p.Class, p.score };//lamda表示法var y = SLIST.Join(ScoreList, //join的 資料表o => o.ID, //主表 join的值p => p.StudentID, //次表  join的值(c, s) =>//主表次表的集合new { c.ID, c.Classroom, c.Name, s.Class, s.score })//所需的欄位 列舉.OrderByDescending(o=> o.score).OrderBy(o => o.Class).OrderBy(o => o.ID).OrderBy(o=>o.Classroom);

Lamda表示法較為複雜

另外需注意的是orderby的順序權重跟T-SQL的剛好相反

三者得到的結果相同

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

莊創偉
莊創偉

Written by 莊創偉

學海無涯。但是為了生計還是得下海的風塵男子

No responses yet

Write a response