Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 2 - Defining a Left Join with LINQ Queries
Defining a Left Join with LINQ Queries
By Paul Kimmel

Rate This Article: Add This Article To:

Defining a Left Join with LINQ Queries
( Page 2 of 4 )

Implementing a Left Join with LINQ

The left join is designed to return parents with and without children from two sources. There is a correlation between parent and child, but no child may actually exist. The challenge is that LINQ supports projections that can be derived from a variety of elements from multiple sequences and these elements are used to initialize the projection. However, if an element is null then initializing a field in the projection is the same as writing any other code to use a null object, an exception is the result.

ADVERTISEMENT

In an inner (or equijoin) uncorrelated elements are removed. In a left join we intentionally want uncorrelated parent elements. (In a right join we want uncorrelated child elements.) Listing 1 shows some classes and extension methods represent a classic music catalog. These classes are Band, Music, a static Helper class with methods for initializing Band and Music objects, and an enumeration representing some musical genres.

Listing 1: Band, Music, Helper classes and an enumeration for genres.

  [Flags]
  public enum Genre
  {
    Unknown,
    Alternative,
    Pop,
    Rock,
    Funk,
    Country,
    Rap,
    Hiphop,
    RhythmAndBlues,
    Classical,
    Jazz,
    Mambo
  }   public static class Helper
  {
    public static Band Add(this List<Band> bands,
    string artist, string genre)
    {
      var result = (from band in bands
                    where
                    band.Artist == artist
                    select band);
      if (result.Count<Band>() > 0)
      {
        Band band = result.First<Band>();
        return band.AddGenre(genre);
      }
      else
      {
        return AddNew(bands, artist, genre);
      }
    }     public static Music Add(this List<Music> songs, int id,     string name,
      string composer, string album)
    {
      Music music = new Music();
      songs.Add(music);
      music.BandID = id;
      music.Composer = composer;
      music.Album = album;
      music.Name = name;       return music;
    }     private static Band AddNew(List<Band> bands, string artist,
    string genre)
    {
      Band band = new Band();
      band.ID = bands.Count + 1;
      band.Artist = artist;
      bands.Add(band);
      return band.AddGenre(genre);
    }   }   public class Band
  {
    public int ID{ get; set; }
    public string Artist{ get; set; }
    public Genre Genre{ get; set; }     public Band AddGenre(string genre)
    {
      try
      {
        object en = Enum.Parse(typeof(Genre), genre, true);
        if (en != null)
          Genre |= (Genre)en;
        return this;
      }
      catch
      {
        return this;
      }
    }
  }   public class Music
  {
    public int BandID{ get; set; }
    public string Name{ get; set; }
    public string Composer { get; set; }
    public string Album{ get; set; }
  }

Listing 1 is pretty boilerplate, plain vanilla code. You may be unfamiliar with the Flags attribute. Flags permits assigning multiple elements of an enumeration to a single bitfield. For our example Flags permits assigning multiple Genres to a single band. The extension methods in the Helper class behave as if they were members of the type following the this keyword in the parameter list. (For more on extension methods check out my article Implementing Extension Methods in C# on this website.)



 
 
>>> More Languages Articles          >>> More By Paul Kimmel
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.