Compare substring in join on clause in CSharp

Description

The following code shows how to compare substring in join on clause.

Example


using System;//from www . j a  v a2  s  .  c om
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

public class MainClass{
   public static void Main(string[] args){   
         String[] QueryString = { "One", "Two", "Three", "Four", "Five",
              "Six", "Seven", "Eight", "Nine", "Ten" };

         String[] IndexArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
              "J", "K", "L", "M", "N", "O", "P", "Q", "R",
              "S", "T", "U", "V", "W", "X", "Y", "Z" };

         var ThisQuery = 
            from StringValue in QueryString 
            join IndexValue in IndexArray
            on StringValue.Substring(0, 1) equals IndexValue
            where Convert.ToChar(IndexValue) > 'F'
            orderby IndexValue
            select new {StringValue, IndexValue};


         foreach (var ThisValue in ThisQuery)
            Console.WriteLine(ThisValue.IndexValue + " - " + ThisValue.StringValue);
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ