Enumerable.Join


If you’re families with Different Types of SQL Joins which are used to query data from more than one tables, LINQ has a JOIN query operator that provide SQL JOIN like behavior (and syntax … depending on which LINQ syntax you prefer Query or Fluent syntax). To drill down LINQ versus SQL join I’d go through Shailendra Chauhan’s blog post. There already is plenty of awesome stuff out there ( esp. for LINQ ) and I’d try to compliment it duplicating. ( see more reading section at the end of this post )

Enumerable.Join let’s you join two sequences based on a property or the relational key property(ies) b/w them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  class Product
  {
    public int productID;
    public Category category;
    public string productName;
    public float price;
  }

  class Category
  {
    public int categoryID;
    public string categoryName;
  }

  class Program
  {
    static void Main(string[] args)
    {
      Category
      Stationary = new Category { categoryID = 1, categoryName = "Stationary" },
      Dairy = new Category { categoryID = 2, categoryName = "Dairy" },
      FastFood = new Category { categoryID = 3, categoryName = "Fast-food" },
      Appearals = new Category { categoryID = 4, categoryName = "Appearals" };

      var products = new Product[] {
          new Product { productID = 1, category = Stationary, productName = "Product 1", price = 0.99f },
          new Product { productID = 1, category = Stationary, productName = "Product 2", price = 0.99f },
          new Product { productID = 1, category = Stationary, productName = "Product 3", price = 0.99f },
          new Product { productID = 1, category = Dairy, productName = "Product 4", price = 0.99f },
          new Product { productID = 1, category = Appearals, productName = "Product 5", price = 0.99f },
          new Product { productID = 1, category = FastFood, productName = "Product 6", price = 0.99f },
          new Product { productID = 1, category = Appearals, productName = "Product 7", price = 0.99f },
          new Product { productID = 1, category = FastFood, productName = "Product 8", price = 0.99f },
          new Product { productID = 1, category = FastFood, productName = "Product 9", price = 0.99f },
          new Product { productID = 1, category = Dairy, productName = "Product 10", price = 0.99f },
          new Product { productID = 1, category = Dairy, productName = "Product 11", price = 0.99f }
  }.ToList<Product>();

      var categories = new Category[] { Stationary, Dairy, FastFood, Appearals }.ToList<Category>();

      var results = categories.Join(
      products,
      category => category,
      product => product.category,
      (category, product) => new
      {
        ItemName = product.productName,
        ItemCategory = category.categoryName
      }
      );
    }
  }
}
More Reading :

We can of course bring Enumerable-s to more complex scenarios and Eric’s blog has a post about it which also refers to http://www.interact-sw.co.uk/iangblog/2010/07/28/linq-cartesian-1, there’s also a post that talks about Some Performance Notes on Enumerable LINQ Operators, http://stackoverflow.com/a/13503860, http://damieng.com/blog/tag/linq etc.

But rather if you’re failrly new to LINQ or Enumerable there are some good posts you might wanna go through, starting from hands on examples from 101 LINQ Examples, a MSDN page that shows plenty of LINQ query examples, a blog post from the guy who created ExtraLINQ nuget package, you might also want to refer to Linq By Example articles, Igor Ostrovsky’s blog etc …

… not in that order 😉

Advertisements

Published by

Aarsh Talati

Software Developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s