How to Join and Filter Two Table through LINQ Query with C#

  1. using System;  
  2. using System.Linq;  
  3. namespace CSharpLINQ  
  4. {  
  5.     class SimpleQuery  
  6.     {  
  7.         static void Main()  
  8.         {  
  9.             var students = new [] // Array of objects of an anonymous type  
  10.                 {  
  11.                     new  
  12.                     {  
  13.                         ID = "101", LName = "Mishra", FName = "Kuldeep", Age = 31, ClassID = "101"  
  14.                     },  
  15.                     new  
  16.                     {  
  17.                         ID = "102", LName = "Sharma", FName = "Ramesh", Age = 20, ClassID = "101"  
  18.                     },  
  19.                     new  
  20.                     {  
  21.                         ID = "103", LName = "Sharma", FName = "Manoj", Age = 21, ClassID = "101"  
  22.                     },  
  23.                     new  
  24.                     {  
  25.                         ID = "104", LName = "Mishra", FName = "Rajesh", Age = 45, ClassID = "101"  
  26.                     },  
  27.                     new  
  28.                     {  
  29.                         ID = "105", LName = "Soni", FName = "Suresh", Age = 35, ClassID = "102"  
  30.                     },  
  31.                     new  
  32.                     {  
  33.                         ID = "106", LName = "Goyal", FName = "Dinesh", Age = 15, ClassID = "102"  
  34.                     },  
  35.                     new  
  36.                     {  
  37.                         ID = "107", LName = "Mishra", FName = "Jignesh", Age = 31, ClassID = "103"  
  38.                     },  
  39.                     new  
  40.                     {  
  41.                         ID = "108", LName = "Sharma", FName = "Mrigesh", Age = 20, ClassID = "103"  
  42.                     },  
  43.                     new  
  44.                     {  
  45.                         ID = "109", LName = "Verma", FName = "Prakash", Age = 21, ClassID = "103"  
  46.                     },  
  47.                     new  
  48.                     {  
  49.                         ID = "110", LName = "Mishra", FName = "Aakash", Age = 45, ClassID = "103"  
  50.                     },  
  51.                     new  
  52.                     {  
  53.                         ID = "111", LName = "Soni", FName = "Dinkar", Age = 35, ClassID = "102"  
  54.                     },  
  55.                     new  
  56.                     {  
  57.                         ID = "112", LName = "Goyal", FName = "Divakar", Age = 15, ClassID = "103"  
  58.                     },  
  59.                     new  
  60.                     {  
  61.                         ID = "113", LName = "Goyal", FName = "Bhaskar", Age = 31, ClassID = "103"  
  62.                     },  
  63.                     new  
  64.                     {  
  65.                         ID = "114", LName = "Sharma", FName = "Tanuj", Age = 20, ClassID = "104"  
  66.                     },  
  67.                     new  
  68.                     {  
  69.                         ID = "115", LName = "Verma", FName = "Anuj", Age = 21, ClassID = "104"  
  70.                     },  
  71.                     new  
  72.                     {  
  73.                         ID = "116", LName = "Goyal", FName = "Dhanush", Age = 45, ClassID = "105"  
  74.                     },  
  75.                     new  
  76.                     {  
  77.                         ID = "117", LName = "Soni", FName = "Mulri", Age = 35, ClassID = "105"  
  78.                     },  
  79.                     new  
  80.                     {  
  81.                         ID = "118", LName = "Goyal", FName = "Manohar", Age = 15, ClassID = "105"  
  82.                     },  
  83.                     new  
  84.                     {  
  85.                         ID = "119", LName = "Mishra", FName = "Sohan", Age = 31, ClassID = "105"  
  86.                     },  
  87.                     new  
  88.                     {  
  89.                         ID = "120", LName = "Sharma", FName = "Ram", Age = 20, ClassID = "101"  
  90.                     },  
  91.                     new  
  92.                     {  
  93.                         ID = "121", LName = "Soni", FName = "Mohan", Age = 21, ClassID = "106"  
  94.                     },  
  95.                     new  
  96.                     {  
  97.                         ID = "122", LName = "Soni", FName = "Rohan", Age = 45, ClassID = "106"  
  98.                     },  
  99.                     new  
  100.                     {  
  101.                         ID = "123", LName = "Soni", FName = "Rahul", Age = 35, ClassID = "101"  
  102.                     },  
  103.                     new  
  104.                     {  
  105.                         ID = "124", LName = "Goyal", FName = "Rohit", Age = 15, ClassID = "106"  
  106.                     }  
  107.                 };  
  108.             var Class = new []  
  109.             {  
  110.                 new  
  111.                 {  
  112.                     ClassID = "101", Class = "I"  
  113.                 },  
  114.                 new  
  115.                 {  
  116.                     ClassID = "102", Class = "II"  
  117.                 },  
  118.                 new  
  119.                 {  
  120.                     ClassID = "103", Class = "III"  
  121.                 },  
  122.                 new  
  123.                 {  
  124.                     ClassID = "104", Class = "IV"  
  125.                 },  
  126.                 new  
  127.                 {  
  128.                     ClassID = "105", Class = "V"  
  129.                 },  
  130.                 new  
  131.                 {  
  132.                     ClassID = "106", Class = "VI"  
  133.                 },  
  134.             };  
  135.             var query = from student in students join cls in Class on student.ClassID equals cls.ClassID where student.ClassID == "103"  
  136.             select student;  
  137.             Console.WriteLine("Only Students of Class \"III\":");  
  138.             foreach(var q in query)  
  139.             {  
  140.                 Console.WriteLine(" {0}, Age: {1}", q.FName, q.Age);  
  141.             }  
  142.         }  
  143.     }  
  144. }  
Output

Jignesh, Age: 31
Mrigesh, Age: 20
Prakash, Age: 21
Aakash, Age: 45
Divakar, Age: 15
Bhaskar, Age: 31