This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var leftExclusiveJoin = Context.Artists.LeftExclusiveJoin(Context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new { ArtistName = artist != null ? artist.Name : "Not Found", AlbumName = album != null ? album.Name : "Not Found" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| namespace JoinExtensionExamples | |
| { | |
| public static class JoinExtension | |
| { | |
| public static IEnumerable<TResult> LeftJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source, | |
| IEnumerable<TInner> inner, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var leftExclusiveJoin = Context.Artists.LeftExcludingJoin(Context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new { ArtistName = artist != null ? artist.Name : "Not Found", AlbumName = album != null ? album.Name : "Not Found" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fullOuterJoin = Context.Artists.FullOuterJoin(Context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new { ArtistName = artist != null ? artist.Name : "Not Found", AlbumName = album != null ? album.Name : "Not Found" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var rightJoin = Context.Artists.RightJoin(Context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new { ArtistName = artist != null ? artist.Name : "Not Found", AlbumName = album != null ? album.Name : "Not Found" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var leftJoin = Context.Artists.LeftJoin(Context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new { ArtistName = artist != null ? artist.Name : "Not Found", AlbumName = album != null ? album.Name : "Not Found" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var innerJoin = Context.Artists.Join(Context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new { ArtistName = artist.Name, AlbumName = album.Name }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| drop table Album | |
| drop table Artist | |
| create table Artist ( Id int identity(1,1) primary key not null, Name varchar(100) not null); | |
| create table Album ( Id int identity(1,1) primary key not null, ArtistId int foreign key references Artist(Id), Name varchar(100) not null); | |
| insert into Artist(Name) values | |
| ('Metallica'), ('Nirvana'), ('Alice in Chains'), ('Stone Temple Pilots'), | |
| ('Smashing Pumpkins'), ('Gregorian Monks'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void Main(string[] args) | |
| { | |
| using (var context = new MusicModel()) | |
| { | |
| context.Database.Log = Console.Write; | |
| var innerJoin = context.Artists.Join(context.Albums, artist => artist.Id, album => album.ArtistId, | |
| (artist, album) => new {ArtistName = artist.Name, AlbumName = album.Name}); | |
| foreach (var item in innerJoin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| create table Artist ( Id int primary key not null, Name varchar(100) not null); | |
| create table Album ( Id int primary key not null, ArtistId int foreign key references Artist(Id), Name varchar(100) not null); | |
| insert into Artist(Name) values ('Metallica'), ('Nirvana'), ('Alice in Chains'), ('Stone Temple Pilots'); | |
| insert into Album (ArtistId, Name) values | |
| (1, 'Kill em all'), | |
| (1, 'And Justice for All'), | |
| (1, 'Ride the Lightning'), | |
| (1, 'Master of Puppets'), |