The Person class is the example business object we will be using with our LINQ queries. There is only a FirstName, LastName, and Age property for the Person class. The code for this class is shown below.
Figure 1.0
public class Person
{
private int _age = 0;
private string _firstName = "";
private string _lastName = "";
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
public Person(string firstName, string lastName, int age)
{
_firstName = firstName;
_lastName = lastName;
_age = age;
}
}
Main Application Code
Since this application is only meant for demonstration purposes I have placed the Person class declaration directly in the main application code for the console application. This is obviously not the way a real application would be written but in this case it reduces complexity and allows us to focus on the LINQ queries themselves.
The first thing we are going to is instantiate a few instances of the Person class. I have used four people here but feel free to play around with it as much as you want. The code for the Person instantiation is below.
Figure 1.1
Person jake = new Person("Jake", "Weakley", 27);
Person bruce = new Person("Bruce", "Campbell", 40);
Person jimmy = new Person("Jimmy", "Dean", 55);
Person brother = new Person("Jimbo", "Weakley", 29);
After instantiating your instances of the People class we will then create a new instance of a generic List class typed as a collection of Person objects and add the people we created into the collection.
Figure 1.2
List
people.Add(jake);
people.Add(bruce);
people.Add(jimmy);
people.Add(brother);
After the setup is complete we can get into writing the LINQ queries.
LINQ: Projection, Filtering, and Sugar
Those of you familiar with writing standard CRUD SQL queries should find the LINQ query syntax fairly easy to grasp after a few runs at it. LINQ is basically like writing a SQL SELECT statement backwards. Using LINQ to SELECT data from a collection is called projection. The WHERE part of the query (called the predicate) is where filtering of that data will occur. To help you wrap your head around it a bit more let us look at a simple example.
We will say, for instance, that we have a table of Person data. This table has 30 rows but we want to only retrieve the rows where Age > 30. We would write the SQL query something like what I have done below.
Figure 1.3
SELECT LastName, FirstName, Age
FROM Person
WHERE Age > 30
The same data would be retrieved using a LINQ query something like the one found below.
Figure 1.4
from p in people
where p.Age > 30
select p;
The two queries are very similar except that in Figure 1.3 the query would be run against a database. In the Figure 1.4 query, we will run the results against an enumerable collection of objects. By now you are probably wondering about what is returned as a result from these queries. Well, lets take a look.
Even though these queries are written to select the exact same data they yield very different results. It should be fairly obvious that the query in Figure 1.3 returns some rows of data either in the query editor window or in some kind of dataset in the data layer of an application. The difference between the return value of the Figure 1.3 query and the Figure 1.4 query is that the LINQ query, in its current state, returns exactly nothing. That’s right, nothing. Why does it return nothing? Let’s find out.
Introduction to LINQ - Part1 Continued...
Final Sample Version of the Project can be download from the final post in this series - "Introduction to LINQ - Part1 (final of many)"
To see all posts on "Introduction to LINQ - Part1" Click on link to your right that says it.
Please do comment the post after you read, Thank you and have a good LINQ.
No comments:
Post a Comment