Thursday, July 2, 2009

Introduction to LINQ - Part1 (final of many)

Working with Result from a LINQ Query
Up until now I have been focusing on the LINQ query itself and different ways to achieve the same result should one so desire to use them. But how do we store and enumerate the results? There are many ways to return type safe data in many different forms but I can’t discuss them all in this scope of this article. In this example I will be using what are called Anonymous Types. Below is an example of using an anonymous type to store the results of a LINQ query as well as the foreach loop we will use to enumerate through the results of the query.
Figure 1.8
var overThirty = from p in people
where p.Age > 30
select p;
foreach (Person p in overThirty)
Console.WriteLine(p.FirstName + " " + p.LastName + ", " + p.Age + " years old.");

The variable “overThirty” is called an anonymous type. Anonymous types are used when the type of return value needs to be inferred by the compiler based on the result of the query. The compiler will infer the type to be returned and create accessors and mutators for the properties in the returned object. In the case of the query in Figure 1.8, we can use the Person type as the inferred type because we queried a List object which only contains Person objects.
So what if we query the List but want to return fields that don’t necessarily exist in the Person object? We come up with our own object on the fly, of course.

Figure 1.9
var overThirtyCustom = from p in people
where p.Age > 30
select new
{
FullName = p.FirstName + " " + p.LastName,
Age = p.Age
};
foreach (var p in overThirtyCustom)
Console.WriteLine(p.FullName + ", " + p.Age + " years old.");

The query in Figure 1.9 will not return a Person object, but an object we created on the fly that now contains a new property called FullName. The FullName property is simply a concatenation of the FirstName and LastName properties separated with a comma. This result set can be enumerated in the same way as the one in Figure 1.8 but will now access the FullName property of our custom object.

Conclusion
Well that wraps up the first part in the introduction to LINQ series. I hope anyone reading this has gotten something out of the article and had a chance to learn something, because I surely have. Keep an eye out for part two the Introduction to LINQ series coming soon. Please send any feedback, complaints, flames or any other comments to loch006@gmail.com or mvsaradhi@yahoo.com.

For More information about LINQ, you may consider the following references

Lambda Functions
http://dotnetaddict.dotnetdevelopersjournal.com/lambda.htm
LINQ Wikipedia Page
http://en.wikipedia.org/wiki/Language_Integrated_Query
MSDN .NET Framework 3.5 “What’s New?”
http://msdn.microsoft.com/en-us/library/bb332048.aspx
Delegates and Events in C# .NET
http://www.akadia.com/services/dotnet_delegates_and_events.html

Introduction to LINQ - Part2 will be updated soon, Stay Tuned...
Final Version of the Project can be download at:

Click Here to download
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.

1 comment:

  1. Hey Vijay,

    A very nice blog you are writing. Cheers Buddy. All the very best and good wishes to you. Keep going

    Inder

    ReplyDelete