ASP.NET - Linq語法應用 GroupJoin

昨天在工作上要使用 Linq實踐出資料庫語法的 Left Join,起初我以為我寫過了
參照幾個月前寫的程式碼,結果發現當初是要實踐出 Inner Join,所以又找了一下資料
並且記錄一下,不然我很快就會忘記了

比起一般的linq語法,Lambda我更喜歡,下面會先展示Lambda的寫法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//先建立兩個類別,之後要組合起來的
class BookId
{
public int Id { get; set; }
public string BookType { get; set; }
}

class Book
{
public int Book_Id { get; set; }
public string Book_Name { get; set; }
}

//給值
BookId[] myBookId = new BookId[]
{
new BookId {Id = 1, BookType = "English"},
new BookId {Id = 2, BookType = "Chinese"}
};

Book[] myBook = new Book[]
{
new Book { Book_Id = 1, Book_Name = "shit school" },
new Book { Book_Id = 1, Book_Name = "English is good" },
new Book { Book_Id = 2, Book_Name = "教你說中文" },
new Book { Book_Id = 2, Book_Name = "從開始到放棄" },
};

Lambda

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var query = myBookId.GroupJoin(myBook, 
a => a.Id, b => b.Book_Id,
(a, ps) => new {
Key = a.BookType,
ps = ps
});

foreach (var item in query)
{
Response.Write(String.Format("Book Type {0}:", item.Key));

foreach (var item2 in item.ps )
{
Response.Write(item2.Book_Name);
}
}

linq

1
2
3
4
5
6
7
8
9
10
var result = from a in myBookId
join b in myBook
on a.Id
equals b.Book_Id into ps
select new {
Key = a.BookType,
ps = ps
};

//我懶了

以上 差不多就是這樣

h0 dl3

groupjoin
LINQ自學筆記-語法應用-聚合資料-Join-3、GroupJoin
LINQ自學筆記-語法應用-聚合資料-DefaultIfEmpty 運算子、實做 Left Outer Join 效果
linqsamples

特別感謝

Google

心得

shit

0%