Here is the second part of my LINQ article , before to that my previous article has one small error.In various places I have mention form it’s a typo the correct word is from (apology for the typo
)
To start my second part lets look at the sample code , here I am trying to demonstrate how efficient and quickly we can play around with collection classes using LINQ .
using System;
using System.Collections.Generic; using System.Text;
using System.Query;using System.Xml.XLinq;
using System.Data.DLinq; namespace QueryExpression{
class Player{
public string PlayerName; public string Country;
public int TotalRuns;public int NoManoftheMatch;
}
class Program {
public List <Player> PlayerList = new List<Player>();static void Main(string[] args)
{
Program pr = new Program(); pr.AddObj(); var queryOutput = from player in pr.PlayerList
where ( player.TotalRuns>=10000) orderby player.TotalRuns descending
select ( new {player.PlayerName,player.TotalRuns,player.NoManoftheMatch} );foreach( var p in queryOutput)
{
Console.WriteLine(“Name : {0} Runs : {1} Man of the Match (no of times) : {2}”,p.PlayerName,p.TotalRuns,p.NoManoftheMatch); }
Console.ReadLine();}//adding the player information
public void AddObj()
{
Player objPlayer1 = new Player{PlayerName= “Sachin”,Country=“Ind”,TotalRuns= 10527 ,NoManoftheMatch=10}; Player objPlayer2 = new Player{PlayerName= “Lara”,Country=“WI”,TotalRuns= 11953,NoManoftheMatch=12};
Player objPlayer3 = new Player{PlayerName= “Ponting”,Country=“AUS”,TotalRuns= 9316,NoManoftheMatch=14};Player objPlayer4 = new Player{PlayerName= “Dravid”,Country=“Ind”,TotalRuns= 9082,NoManoftheMatch=9};
Player objPlayer5 = new Player{PlayerName= “Inzi”,Country=“Pak”,TotalRuns= 8615,NoManoftheMatch=8}; Player objPlayer6 = new Player{PlayerName= “Kallis”,Country=“SA”,TotalRuns= 8072,NoManoftheMatch=16};
Player objPlayer7 = new Player{PlayerName= “Langer”,Country=“Aus”,TotalRuns= 7623,NoManoftheMatch=8};Player objPlayer8 = new Player{PlayerName= “SWaugh”,Country=“Aus”,TotalRuns= 10527,NoManoftheMatch=14};
Player objPlayer9 = new Player{PlayerName= “Gavaskar”,Country=“Ind”,TotalRuns= 10122,NoManoftheMatch=5};
Player objPlayer10 = new Player{PlayerName= “Border”,Country=“Aus”,TotalRuns= 11174,NoManoftheMatch=11};
PlayerList.Add(objPlayer1);
PlayerList.Add(objPlayer2); PlayerList.Add(objPlayer3);
PlayerList.Add(objPlayer4);
PlayerList.Add(objPlayer5);
PlayerList.Add(objPlayer6);
PlayerList.Add(objPlayer7);
PlayerList.Add(objPlayer8);
PlayerList.Add(objPlayer9);
PlayerList.Add(objPlayer10);
}
}
}
If you look at the select or where this is nothing but the extension methods defined in System.Query and new{…} is nothing anonymous type and object initializer and in foreach loop I have used implicit typing of local variables , without local variable type its very hard to get the required output because we may not know the possible type of the data returned . LINQ provides lots other options/clauses such as join, groupby, distinct, union, any, all and aggregated functions ( sum, count, average, max, min, aggregate) etc to support structure query language programming model more strongly .I am concluding this session here and suggest you all top start explore LINQ .If you have any doubt please feel free to post your queries to me – anandkumar2004@gmail.com
Cheers
Anand