If you want to access only specific properties from WebSite, it can be achieved using LINQ.
Code:
How to: Query an ArrayList with LINQ
LINQ to query non-generic IEnumerable collections such as ArrayList, you must explicitly declare the type of the range variable to reflect the specific type of the objects in the collection.
Here I have taken Cricket 's Player class to understand the Linq to Object.
Player contains single properties FirstName,LastName,Country and Multivalue Properties like Scores.List of batsman who have scrored more than 50 runs in their First Match
public class Player
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public int[] Scores { get; set; }
public int ManOfTheSeries { get; set; }
}
class Program
{
static void Main(string[] args)
{
IList<Player> playerList = new List<Player>()
{
new Player
{
FirstName = "Sachin",
LastName = "Tendulkar",
Country= "India",
Scores = new int[] { 200, 190, 170,150 }
},
new Player
{
FirstName = "Ricky",
LastName = "Ponting",
Country = "Australia",
Scores = new int[] { 180, 84, 70, 142 }
},
new Player
{
FirstName = "Kumar",
LastName = "Sangakara",
Country = "Sri Lanka",
Scores = new int[] { 5, 80, 5, 39 }
},
new Player
{
FirstName = "Hashim",
LastName = "Amla",
Country = "South Africa",
Scores = new int[] { 88, 94, 65, 91 }
},
new Player
{
FirstName = "Mahela",
LastName = "Jayawardene",
Country = "Sri Lanka",
Scores = new int[] { 97, 89, 85, 82 }
}
};
var query = from Player player in playerList
where player.Scores[0] >50
select player;
Console.WriteLine("List of batsman who have scrored more than 50 runs in their First Match");
foreach (Player s in query)
Console.WriteLine(s.FirstName +" " + s.LastName + ": " + s.Scores[0]);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Output
Sachin Tendulkar: 200
Ricky Ponting: 180
Hashim Amla: 88
Mahela Jayawardene: 97
Press any key to exit.
To get same output with query operator,
Replace above query part with below code.
Console.WriteLine("FILTER- List of batsman who have scrored more than 50 runs in their First Match");
var queryFilter = playerList.Where(a => a.Scores[0] > 50).Select(a => a);Linq And String
http://msdn.microsoft.com/en-us/library/bb397915.aspx
LINQ and File Directories
http://msdn.microsoft.com/en-us/library/bb397911.aspx
LINQ and Reflection
http://msdn.microsoft.com/en-us/library/bb397929.aspx
How to: Add Custom Methods for LINQ Queries
![]() |