Skip to content

Instantly share code, notes, and snippets.

@kitlabcode
kitlabcode / LINQ Left Exclusive Join
Created May 24, 2015 19:29
LINQ Left Exclusive Join
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" });
@kitlabcode
kitlabcode / LINQ Join Extensions
Created May 24, 2015 19:27
LINQ Join Extensions
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,
@kitlabcode
kitlabcode / LINQ Left Exclusive Join
Created May 24, 2015 19:17
LINQ Left Exclusive Join
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" });
@kitlabcode
kitlabcode / LINQ Full Outer Join Example
Created May 24, 2015 18:59
LINQ Full Outer Join Example
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" });
@kitlabcode
kitlabcode / LINQ Right Join Example
Created May 24, 2015 18:50
LINQ Right Join Example
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" });
@kitlabcode
kitlabcode / LINQ Left Join Example
Created May 24, 2015 18:48
LINQ Left Join Example
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" });
@kitlabcode
kitlabcode / Inner Join
Created May 24, 2015 18:45
LINQ Inner Join Default
var innerJoin = Context.Artists.Join(Context.Albums, artist => artist.Id, album => album.ArtistId,
(artist, album) => new { ArtistName = artist.Name, AlbumName = album.Name });
@kitlabcode
kitlabcode / gist:9d288c39ebaf13aa3361
Created May 24, 2015 17:08
Create two tables, Artist and Album
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');
@kitlabcode
kitlabcode / LINQ Default Join
Last active August 29, 2015 14:21
LINQ Default Join
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)
@kitlabcode
kitlabcode / gist:ff57b88bfc82f75dc664
Created May 24, 2015 00:29
Create basic artist and album table
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'),