in

This site is all about the amazingly cool stuff you can do with VB.NET.

Fun with VB LINQ

Deferred Execution

Here's a quick and dirty example of LINQ to Objects that my friend Justin showed me. Actually, he showed me in C#, but it took all of about 45 seconds to rewrite it in VB.NET so here it is:

1. Create an ASPX page in Visual Studio and add a GridView Control. Leave the name as GridView1.
(You can do this any number of ways, like a console app, but I happened to do it in an aspx page.)

2. In your codebehind page, add the following to the form1.Load event:

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
    Dim filter As String = "System"
    Dim query = From a In AppDomain.CurrentDomain.GetAssemblies() _
                Where a.GetName().Name.Contains(filter) _
                Select a.GetName.Name

    'filter = "Xml"

    GridView1.DataSource = query
    GridView1.DataBind()
End Sub

Be sure to leave the 'filter = "Xml" line commented out for now, we'll come back to that in a minute!! 

Now run the page. You should see something that looks like this:

image

You should be looking at a list of Namespaces that begin with "System."  Once you're done ooh'ing and ahh'ing over it, go back to your codebehind page and...

3. uncomment the 'filter = "Xml" line.

Now you're probably thinking, so what Chris, it's not going to do anything because it's AFTER the query...  But that is where you'd be wrong. Suspend your disbelief for a moment and take my word for it. I'll explain in a second.

So, if you uncomment that line, and run the code again, this time you should see something like this:

image

I know, crazy huh?

What happens is this... when you create your query, it's not actually executing until you need it. In this case, it's the databind. In other cases, it might be when you call the .ToArray method of your resultset, or when you add the contents to a custom object, or the .Count method, etc...

See... there's this thing called Deferred Execution, and it's part of LINQ. It gives us a lot of flexibility. If you need to stick different peices of data from the same source in multiple places, for example, build your query, databind your grid, change that ONE line of code, databind your other grid... awesome, huh?

Enjoy!

(For a much more detailed explanation, I recommend heading over to this MSDN article:  Overview of Visual Basic 9.0  It's about 2/3rds of the way down, but the whole article is good reading. It's by Erik Meijer, Amanda Silver, and Paul Vick.)

Published Mar 24 2008, 12:57 PM by admin
Filed under: , ,

Comments

No Comments

About admin

Chris Williams is a Technology Evangelist for Magenic. He is the founder of several .NET User Groups on the east coast, and most recently the Twin Cities XNA User Group (www.twincitiesxnausergroup.com) in Minneapolis, MN. He is a VB.NET MVP, rabid blogger and owner of www.ILoveVB.NET. He's also a MCT, MCSD (.NET) Early Adopter, MCAD, freelance game developer, occasional author, tech editor, code camp & user group speaker, vintage arcade game collector and plays a pretty mean guitar in Rock Band.
Copyright 2008 - ILoveVB.NET
Powered by Community Server (Commercial Edition), by Telligent Systems