LINQ Join Operation Sample

By TSN ...

For the sample i have taken theis generic list of type Player and Player_Data , which i will use this Lists as collections and perform the joins operations using Linq.

List<Player> Players = new List<Player>
            {
                 new Player {P_Id =1,Last ="Smith",First ="John",Team_Id =1,Number =12},
                 new Player {P_Id =2,Last ="Bobbit",First ="Billy",Team_Id =1,Number =2},
                 new Player {P_Id =3,Last ="Hurta",First ="Wil",Team_Id =2,Number =32},
                 new Player {P_Id =4,Last ="Ouchy",First ="Tim",Team_Id =2,Number =22},
                 new Player {P_Id =5,Last ="Byrd",First ="Eric",Team_Id =3,Number =6},
                 new Player {P_Id =6,Last ="Jordan",First ="Ryan",Team_Id =4,Number =23},
                 new Player {P_Id =7,Last ="Hammer",First ="Wally",Team_Id =4,Number =21},
                 new Player {P_Id =8,Last ="Hammer",First ="Ron",Team_Id =4,Number =44},
                 new Player {P_Id =11,Last ="KnotGood",First ="Al",Number =0}
            };

           List<Player_Data> Players_Data = new List<Player_Data>
             {
                 new Player_Data {P_Id =1,Weight =71,Height =120},
                 new Player_Data {P_Id =2,Weight =58,Height =195},
                 new Player_Data {P_Id =3,Weight =72,Height =200},
                 new Player_Data {P_Id =4,Weight =74,Height =170},
                 new Player_Data {P_Id =5,Weight =71,Height =182},
                 new Player_Data {P_Id =6,Weight =72,Height =189},
                 new Player_Data {P_Id =7,Weight =79,Height =250},
                 new Player_Data {P_Id =8,Weight =73,Height =193},
                 new Player_Data {P_Id =11,Weight =85,Height =310}
            };
            
             #region This is To find Tallest Player in the list and Concatenate the Firstname and Lastnameto give a single nameas output
            
                 var f = from pd in Players_Data
                        join pl in Players
                        on pd.P_Id equals pl.P_Id
                          where pd.Height == Players_Data.Max(p => p.Height)
                        select new { NAME = pl.First + " " + pl.Last };
                  Console .WriteLine ("TALLEST PLAYERS IN THE LIST :");
                 foreach (var i in f)
                      Console.WriteLine(i.NAME);
    
                  Console.ReadLine();
    
             #endregion

LINQ Join Operation Sample  (1228 Views)