Categories
Search Box
- Webforumz
- Unmoderated Forums
- CSS Articles & Tips
- FMFA Manager
- The Net is Dying
- RandomRabbits
- Total Development
Latest Posts
- My Organic Modem
- Temporary Fix for the KB940510 windows update
- Practical Example of using LINQ
- Getting Started with Programming (Beginners Guide)
- Hardware Maintenance
Practical Example of using LINQ
A Practical Example of Using LINQ. Some of you may know that LINQ is an SQL method of querying in the .NET framework, if you are new to this I recommend an article on Kirupa.com "An introduction to LINQ".
When I first discovered LINQ I thought it looked overly complex for something that I'd have little use for but on discovering more about it's potential I saw how powerful LINQ could be in my applications. For this reason I've compiled a sample application that uses LINQ to query an array of "People" dependent on their Age and the first letters of there Name. This is the Person class that each "person" is assigned to:
public class Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
this.Name = name;
this.Age = age;
}
public override string ToString()
{
return this.Name + " is " + this.Age + " years old.";
}
}
As you can see this class is very simple and to keep code clean we can find the name and age of the person by calling the "ToSting()" method.
You can download the project to see the source code. The main piece of code we are looking at is the LINQ query, this is where we found out how useful and practical LINQ can be.
IEnumerable qry = from person in people
where person.Age > GreaterThan & person.Age < LessThan & person.Name.StartsWith(StartW)
select person;
You may not be familiar with the data type of "qry" but put simply it's a collection of the Person objects that are returned by the LINQ query. Okay so the first part of the query "From person in people", this tells LINQ what array we are dealing with and what we will refer to each object in the array as. I simply named each object "person" but you could use any other valid variable name such as "p" or "peep".
Next line is where the conditionals come in, firstly we check for all the Person objects that have an age greater than the integer the user povides, if you find that hard to grasp it is as though we are just checking the age of a person object but this time we are checking all of the ages in an array. Next we check that the persons age is less than the age provided, which is easy to understand if you understand that previous conditional. Now we have something pretty cool, we can check that the "Persons" name starts with a letter or combination of letters, this means that if the StartW variable contains "Al" then my name would be returned providing that I meet the previous two conditions.
Finally we select all the objects that satisfy these condtions (providing that there are some!), he if we just wanted to the name of the Person that satisfied these conditions we would use "select person.Name" and would change the data type to "IEnumerable
Now that we have the results of the LINQ query, we can simply loop through them like so, printing the name and age of each Person that satisfied the conditions.
foreach (Person p in qry)
Console.WriteLine(p.ToString());
And there you have a practical example of LINQ!