Join Clause in LINQ

OIN clause is very useful when merging more than two table or object data into a single unit. It combines different source elements into one and also creates a relationship between them.

using System;
using System.Linq;

namespace CSharpLINQ
    class SimpleQuery
		static void Main( )
			var students = new[] // Array of objects of an anonymous type
				new { ID="101", LName="Mishra", FName="Kuldeep", Age=31, ClassID="101"},
				new { ID="102", LName="Sharma", FName="Ramesh", Age=20, ClassID="101"},
				new { ID="103", LName="Sharma", FName="Manoj", Age=21, ClassID="101"},
				new { ID="104", LName="Mishra", FName="Rajesh", Age=45, ClassID="101"},
				new { ID="105", LName="Soni", FName="Suresh", Age=35, ClassID="102"},
				new { ID="106", LName="Goyal", FName="Dinesh", Age=15, ClassID="102"},
				new { ID="107", LName="Mishra", FName="Jignesh", Age=31, ClassID="103"},
				new { ID="108", LName="Sharma", FName="Mrigesh", Age=20, ClassID="103"},
				new { ID="109", LName="Verma", FName="Prakash", Age=21, ClassID="103"},
				new { ID="110", LName="Mishra", FName="Aakash", Age=45, ClassID="103"},
				new { ID="111", LName="Soni", FName="Dinkar", Age=35, ClassID="102"},
				new { ID="112", LName="Goyal", FName="Divakar", Age=15, ClassID="103"},
				new { ID="113", LName="Goyal", FName="Bhaskar", Age=31, ClassID="103"},
				new { ID="114", LName="Sharma", FName="Tanuj", Age=20, ClassID="104"},
				new { ID="115", LName="Verma", FName="Anuj", Age=21, ClassID="104"},
				new { ID="116", LName="Goyal", FName="Dhanush", Age=45, ClassID="105"},
				new { ID="117", LName="Soni", FName="Mulri", Age=35, ClassID="105"},
				new { ID="118", LName="Goyal", FName="Manohar", Age=15, ClassID="105"},
				new { ID="119", LName="Mishra", FName="Sohan", Age=31, ClassID="105"},
				new { ID="120", LName="Sharma", FName="Ram", Age=20, ClassID="101"},
				new { ID="121", LName="Soni", FName="Mohan", Age=21, ClassID="106"},
				new { ID="122", LName="Soni", FName="Rohan", Age=45, ClassID="106"},
				new { ID="123", LName="Soni", FName="Rahul", Age=35, ClassID="101"},
				new { ID="124", LName="Goyal", FName="Rohit", Age=15, ClassID="106"}
			var Class = new[]
				new { ClassID="101" , Class="I"},
				new { ClassID="102" , Class="II"},
				new { ClassID="103" , Class="III"},
				new { ClassID="104" , Class="IV"},
				new { ClassID="105" , Class="V"},
				new { ClassID="106" , Class="VI"},

			var query = from student in students join cls in Class on student.ClassID equals cls.ClassID where student.ClassID=="103" select student;
			Console.WriteLine("Only Students of Class \"III\":");
			foreach (var q in query)
				Console.WriteLine("    {0},  Age: {1}", q.FName, q.Age);

0 Comment's

Comment Form