An Introduction to LINQ - Implicitly Typed Variables and Anonymous Types
(Page 3 of 4 )
LINQ is where implicitly typed variables shine. Notice how we stored the results in an IEnumerable<Person>. This, of course, works fine, but we're able to shorten it a bit:
var overThirty = from p in people
where p.Age > 30
select p;
Dim overThirty = From p In people _
Where p.Age > 30 _
Select p
Notice how we return the results as Person objects. This may be appropriate in our example, but consider the case of a relational database or an XML file. We need to represent each piece of data as an object, and we also may not need all of the fields. While in the above example we already had an obvious type handy to represent our data, we could also have used an anonymous type to represent our data.
Anonymous types are used like this:
var mike = new { Name = "Mike", Age = 51, Phone = "555-232-2341" };
Dim mike = New With {.Name = "Mike", .Age = 51, .Phone = "555-232-2341"}
Anonymous types make sense within the context of LINQ. The following code yields the same results (or, rather, approximately the same, as we'll soon see) as the previous code, but it uses an anonymous type:
var overThirty = from p in people
where p.Age > 30
select new { Name = p.Name, Age = p.Age, Phone = p.Phone };
Dim overThirty = From p In people _
Where p.Age > 30 _
Select New With {.Name = p.Name, .Age = p.Age, .Phone = p.Phone}
Now, instead of getting a collection of Person objects as a result, we get a collection of objects of an anonymous type. These have to be treated a bit differently. For example, suppose we wanted to loop over the results and print them out. Before, we could do something like this:
foreach (Person p in overThirty)
{
Console.WriteLine("Name: {0}", p.Name);
Console.WriteLine("Age: {0}", p.Age);
Console.WriteLine("Phone: {0}", p.Phone);
}
For Each p As Person In overThirty
Console.WriteLine("Name: {0}", p.Name)
Console.WriteLine("Age: {0}", p.Age)
Console.WriteLine("Phone: {0}", p.Phone)
Next
Using the anonymous type, though, we can't treat each element as a Person object. We can't match it to a specific type, but that's okay because we know the type's properties and can access them in the same way:
foreach (var p in overThirty)
{
Console.WriteLine("Name: {0}", p.Name);
Console.WriteLine("Age: {0}", p.Age);
Console.WriteLine("Phone: {0}", p.Phone);
}
For Each p In overThirty
Console.WriteLine("Name: {0}", p.Name)
Console.WriteLine("Age: {0}", p.Age)
Console.WriteLine("Phone: {0}", p.Phone)
Next
The only difference is that in C#, Person has been replaced with var, and in VB.NET, the type declaration has been taken out entirely.
In the above example, we chose to make our anonymous type's properties mirror those of the real type, Person. However, we could have made them different. For example, if we only need the name and number, then we're free to leave the Age property out completely, or if we want to add a new property, such as the year in which the person was born, then we're free to add that and then compute the value of it. Anonymous types are especially useful when the data source provides numerous fields, of which you only need to use a few.
Next: More Standard Query Operators >>
More .NET Articles
More By Peyton McCullough