Saturday, May 19, 2012
Forum Minimize
 
Web Development & Design ForumWeb Development & Design ForumLinqLinqGeneralGeneralLINQ join clause LINQ join clause
Previous Previous
 
Next
 Disabled
New Post
 9/7/2010 4:48 PM
 
The join clause is useful for associating elements from different source sequences that have no direct relationship in the object model. The only requirement is that the elements in each source share some value that can be compared for equality. For example, a distributor might have a list of suppliers of a certain product and a list of buyers. A join clause can be used, for example, to create a list of the suppliers and buyers of that product who are in the same specified region.
A join clause takes two source sequences as input. The elements in each sequence must either be or contain a property that can be compared to a corresponding property in the other sequence. The join clause compares the specified keys for equality by using the special equals keyword. All join performed by the join clause are equijoins. The shape of the output of a join clause depends on the specific type of join you are performing. The following are three most common join types:
  • Inner join
  • Group join
  • Left outer join
The following example shows a simple join clause query.
using System;
using System.Linq;
using System.Windows.Forms;
 
namespace Linq_Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnTest_Click(object sender, EventArgs e)
        {
            //Create an array as a data source
            Int32[] ArrayA = { 1, 2, 3, 4, 5, 8, 9, 10 };
            Int32[] ArrayB = { 1, 3, 5, 6, 7, 8 };
 
            //Define the query
            var Joined = from QueryA in ArrayA
                         join QueryB in ArrayB
                         on QueryA equals QueryB
                         select new {QueryA, QueryB};
               
            //Display the result
            txtResult.Text = txtResult.Text + "\r\nJoin Results:\r\n\r\n";
 
            foreach (var OutPair in Joined)
                txtResult.Text = txtResult.Text + OutPair.QueryA.ToString() + " - " +
                    OutPair.QueryB.ToString() + "\r\n";
        }
    }
}
Output

 

Previous Previous
 
Next
 Disabled
Web Development & Design ForumWeb Development & Design ForumLinqLinqGeneralGeneralLINQ join clause LINQ join clause

spacer