<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://linqinaction.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Wooley's LINQ Wonderings : LINQ, VB</title><link>http://linqinaction.net/blogs/jwooley/archive/tags/LINQ/VB/default.aspx</link><description>Tags: LINQ, VB</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Annoucing ThinqLinq.com</title><link>http://linqinaction.net/blogs/jwooley/archive/2008/01/10/annoucing-thinqlinq-com.aspx</link><pubDate>Thu, 10 Jan 2008 05:46:00 GMT</pubDate><guid isPermaLink="false">474e9fbe-b00d-4298-b93d-72ccda50e72d:2184</guid><dc:creator>jwooley</dc:creator><slash:comments>0</slash:comments><comments>http://linqinaction.net/blogs/jwooley/comments/2184.aspx</comments><wfw:commentRss>http://linqinaction.net/blogs/jwooley/commentrss.aspx?PostID=2184</wfw:commentRss><wfw:comment>http://linqinaction.net/blogs/jwooley/rsscomments.aspx?PostID=2184</wfw:comment><description>&lt;P&gt;You may have seen me present it at a speaking engagement. You may have watched the &lt;A href="http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2007/06/28/asp-net-podcast-show-95-jim-wooley-on-linq-part-i-video-and-audio.aspx"&gt;podcasts&lt;/A&gt;. You may have even &lt;A href="http://www.thinqlinq.com/Downloads/ThinqLinqRtm.zip"&gt;downloaded the sample application&lt;/A&gt;. Now you can see it in action. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.thinqlinq.com"&gt;&lt;STRONG&gt;ThinqLinq.com&lt;/STRONG&gt;&lt;/A&gt;&lt;STRONG&gt; is now live.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The site was designed completely in VB with LINQ as the data access mechanism.&amp;nbsp;The base application was built in 2 hours from not knowing RSS to being able to import a RSS feed, displaying it on a form and producing a new feed from the imported data. The site is a testimate to the power of LINQ and the RAD capabilities that it brings.&amp;nbsp;Head on over to the site and check it out.&lt;/P&gt;
Crossposted from http://devauthority.com/blogs/jwooley/default.aspx&lt;img src="http://linqinaction.net/aggbug.aspx?PostID=2184" width="1" height="1"&gt;</description><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/VB+DevCenter/default.aspx">VB DevCenter</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/VB/default.aspx">VB</category></item><item><title>VS 2008 Beta 2 VPC images will timebomb on 11/1/2007</title><link>http://linqinaction.net/blogs/jwooley/archive/2007/10/26/vs-2008-beta-2-vpc-images-will-timebomb-on-11-1-2007.aspx</link><pubDate>Fri, 26 Oct 2007 20:13:00 GMT</pubDate><guid isPermaLink="false">474e9fbe-b00d-4298-b93d-72ccda50e72d:1617</guid><dc:creator>jwooley</dc:creator><slash:comments>0</slash:comments><comments>http://linqinaction.net/blogs/jwooley/comments/1617.aspx</comments><wfw:commentRss>http://linqinaction.net/blogs/jwooley/commentrss.aspx?PostID=1617</wfw:commentRss><wfw:comment>http://linqinaction.net/blogs/jwooley/rsscomments.aspx?PostID=1617</wfw:comment><description>&lt;P&gt;If you downloaded the VPC image version of Visual Studio 2008 beta 2, make sure to back-up your data and move anything off of the Team Foundation Server before November 1, 2007. I was just informed that the images will be time bombing on November 1 2007 rather than the originally intended March 15, 2008 date. If you don't download it now, you may not be able to retrieve your projects. More information is available at the &lt;A href="http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx"&gt;VS 2008 beta 2 download site&lt;/A&gt;. If you installed the stand-alone version, you should be ok.&lt;/P&gt;
Crossposted from http://devauthority.com/blogs/jwooley/default.aspx&lt;img src="http://linqinaction.net/aggbug.aspx?PostID=1617" width="1" height="1"&gt;</description><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/VB/default.aspx">VB</category></item><item><title>Adding RSS posts to ThinqLinq using System.ServiceModel.Syndication.SyndicationFeed</title><link>http://linqinaction.net/blogs/jwooley/archive/2007/10/12/adding-rss-posts-to-thinqlinq-using-system-servicemodel-syndication-syndicationfeed.aspx</link><pubDate>Fri, 12 Oct 2007 02:58:00 GMT</pubDate><guid isPermaLink="false">474e9fbe-b00d-4298-b93d-72ccda50e72d:1523</guid><dc:creator>jwooley</dc:creator><slash:comments>0</slash:comments><comments>http://linqinaction.net/blogs/jwooley/comments/1523.aspx</comments><wfw:commentRss>http://linqinaction.net/blogs/jwooley/commentrss.aspx?PostID=1523</wfw:commentRss><wfw:comment>http://linqinaction.net/blogs/jwooley/rsscomments.aspx?PostID=1523</wfw:comment><description>&lt;P&gt;When I originally started the ThinqLinq project I began by loading the RSS feed from my &lt;A href="http://linqinaction.net/blogs/jwooley"&gt;DevAuthority blog&lt;/A&gt;, and iterating over the results adding them to the PostItems table in the data context. With LINQ this is relatively easy. Loading the XML from the feed is done with a single line:&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;Dim&lt;/FONT&gt; Feed &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; System.Xml.Linq.XDocument = XDocument.Load(&lt;FONT color=#a31515&gt;"http://devauthority.com/blogs/jwooley/rss.aspx"&lt;/FONT&gt;&lt;FONT size=4&gt;&lt;FONT size=3&gt;)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;The xml document consists some basic informational elements which are not terribly important in this instance as we are only pulling from a single blog. Following the initial elements, the document contains a series of "item" elements that contain the actual post information. We can easily query the document and return an IEnumerable(Of XElement) object that we can iterate over and create new post items. Below is an excerpt from my original implementation.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;&lt;FONT color=#0000ff&gt;For&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Each&lt;/FONT&gt; post &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; XElement &lt;FONT color=#0000ff&gt;In&lt;/FONT&gt; Feed...&lt;FONT color=#6464b9&gt;&amp;lt;&lt;/FONT&gt;item&lt;/FONT&gt;&lt;FONT size=3&gt;&lt;FONT color=#6464b9&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; DataPost &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; LinqBlog.BO.PostItem&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataPost.Author = &lt;/FONT&gt;&lt;FONT size=3&gt;&lt;FONT color=#a31515&gt;"Jim Wooley"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;DataPost.Description = post.Element(&lt;FONT color=#a31515&gt;"description"&lt;/FONT&gt;).Value&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataPost.PublicationDate = &lt;FONT color=#0000ff&gt;CDate&lt;/FONT&gt;(post.Element(&lt;FONT color=#a31515&gt;"pubDate"&lt;/FONT&gt;).Value)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataPost.Title = post.Element(&lt;FONT color=#a31515&gt;"title"&lt;/FONT&gt;).Value&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dc.PostItems.Add(DataPost)&lt;BR&gt;&lt;FONT color=#0000ff&gt;Next&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;&lt;FONT color=#000000 size=3&gt;Once the records are added to the table, we can commit them to the database with a single call to SubmitChanges as follows:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff size=4&gt;&lt;FONT size=4&gt;
&lt;P&gt;&lt;FONT size=3&gt;dc.SubmitChanges()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;Ok, so that is easy enough.&amp;nbsp;There are a&amp;nbsp;couple of things to mention before we continue on. The .Add method will be changed to .InsertOnSubmit when LINQ and the .NET 3.5 Framework is released. This will be a breaking change for anyone who is currently working with the beta builds.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;Another item of note: this implementation does not bother importing the various sub-collections, including attachments, comments, categories, etc. We will address some of those in a future update.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;In looking back at the code and being more familiar with LINQ, it is about time to update this code taking advantage of some of LINQ's more interesting features. First VB 9 allows us to&amp;nbsp;eliminate some of the functional construction syntax. Instead of weakly accessing the post.Element("description").Value, we can refer to post.&amp;lt;description&amp;gt;.Value. If we import the namespace, we will even get intellisense on the xml document.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;In addition, rather than iterating over the item elements explicitly, we can use a &amp;nbsp;LINQ query to create an IEnumerable(Of PostItem) list using the object initializers in the select projection. We then fill the entire collection using the table's AddAll (changing to InsertAllOnSubmit with RTM). With this change, we eliminate the entire for each loop. Below is the revised code:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT size=3&gt;Dim&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=3&gt;&lt;FONT color=#000000&gt; Feed &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;As&lt;/FONT&gt;&lt;FONT color=#000000&gt; System.Xml.Linq.XDocument = XDocument.Load(&lt;/FONT&gt;&lt;FONT color=#a31515&gt;"http://devauthority.com/blogs/jwooley/rss.aspx"&lt;/FONT&gt;&lt;FONT color=#000000&gt;)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; dc &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; LinqBlogDataContext()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;dc.PostItems.AddAll(&lt;FONT color=#0000ff&gt;From&lt;/FONT&gt; post &lt;FONT color=#0000ff&gt;In&lt;/FONT&gt; Feed...&lt;FONT color=#6464b9&gt;&amp;lt;&lt;/FONT&gt;item&lt;FONT color=#6464b9&gt;&amp;gt;&lt;/FONT&gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;Select&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; PostItem &lt;FONT color=#0000ff&gt;With&lt;/FONT&gt; { _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Author = &lt;FONT color=#a31515&gt;"Jim Wooley"&lt;/FONT&gt;, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Description = post.&lt;FONT color=#6464b9&gt;&amp;lt;&lt;/FONT&gt;description&lt;FONT color=#6464b9&gt;&amp;gt;&lt;/FONT&gt;.Value, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PublicationDate = &lt;FONT color=#0000ff&gt;CDate&lt;/FONT&gt;(post.&lt;FONT color=#6464b9&gt;&amp;lt;&lt;/FONT&gt;pubDate&lt;FONT color=#6464b9&gt;&amp;gt;&lt;/FONT&gt;.Value), _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Title = post.&lt;FONT color=#6464b9&gt;&amp;lt;&lt;/FONT&gt;title&lt;FONT color=#6464b9&gt;&amp;gt;&lt;/FONT&gt;.Value})&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;dc.SubmitChanges()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;That's it. We've read the feed from the site, filled the object collection and saved the resulting objects to the database. Clean and simple.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;But hold on... The title of this post refers to the System.ServiceModel.Syndication.SundicationFeed object. This is a new object as part of the WCF enhancements in the .NET 3.5 Framework. To use it, add a reference to the System.ServiceModel.Web library. This object lets you create and consume feeds in both RSS and ATOM formats and use a single object model against both options. It also gives easy access to a number of child object structures, including Authors, Categories, Contributors, and&amp;nbsp;Links. Additionally it strongly types the results so that we don't need to explicitly cast the values ourselves (for example with the PublicationDate). Here is the complete code to load the feed using the SyndicationFeed.Load method, fill the PostItem collection and submit them to the database.&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=4&gt;
&lt;P&gt;&lt;FONT size=3&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; feed &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; SyndicationFeed = SyndicationFeed.Load(&lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; System.Uri(&lt;FONT color=#a31515&gt;"http://devauthority.com/blogs/jwooley/rss.aspx"&lt;/FONT&gt;))&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;&lt;FONT color=#0000ff&gt;Dim&lt;/FONT&gt; dc &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; LinqBlogDataContext()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;dc.PostItems.AddAll(&lt;FONT color=#0000ff&gt;From&lt;/FONT&gt; p &lt;FONT color=#0000ff&gt;In&lt;/FONT&gt; feed.Items _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;Select&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt; PostItem &lt;FONT color=#0000ff&gt;With&lt;/FONT&gt; _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {.Author = &lt;FONT color=#0000ff&gt;If&lt;/FONT&gt;(p.Authors.Count &amp;gt; 0, p.Authors(0).Name, &lt;FONT color=#a31515&gt;"Jim Wooley"&lt;/FONT&gt;), _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Description = p.Summary.Text, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PublicationDate = p.PublishDate, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Title = p.Title.Text})&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=3&gt;dc.SubmitChanges()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;The code is substantially the same as the revised version using the XML Literals above. The advantage of using the Syndication services implementation is that it abstracts the feed structure (RSS/ATOM), giving direct and strongly typed access to the contents.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000 size=3&gt;Now that we've set this up, maybe I can work on using the SyndicationFeed to generate the feeds in ThinqLinq and present that in a future post. Stay tuned.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=4&gt;&lt;FONT size=3&gt;Technorati tags: &lt;/FONT&gt;&lt;A href="http://technorati.com/tag/linq" rel=tag&gt;&lt;FONT size=3&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=linq"&gt;&lt;FONT color=#444433&gt;linq&lt;/FONT&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3&gt;, &lt;/FONT&gt;&lt;A href="http://technorati.com/tag/orcas" rel=tag&gt;&lt;FONT size=3&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=orcas"&gt;&lt;FONT color=#444433&gt;orcas&lt;/FONT&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3&gt;, &lt;/FONT&gt;&lt;A href="http://technorati.com/tag/vb" rel=tag&gt;&lt;FONT size=3&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=vb"&gt;&lt;FONT color=#444433&gt;vb&lt;/FONT&gt;&lt;/FONT&gt;&lt;/A&gt;, &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;A href="http://technorati.com/tag/xml" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=xml"&gt;xml&lt;/A&gt;&lt;/P&gt;
Crossposted from http://devauthority.com/blogs/jwooley/default.aspx&lt;img src="http://linqinaction.net/aggbug.aspx?PostID=1523" width="1" height="1"&gt;</description><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/VB/default.aspx">VB</category></item><item><title>Code Camp 2007 downloads</title><link>http://linqinaction.net/blogs/jwooley/archive/2007/10/08/code-camp-2007-downloads.aspx</link><pubDate>Mon, 08 Oct 2007 23:22:00 GMT</pubDate><guid isPermaLink="false">474e9fbe-b00d-4298-b93d-72ccda50e72d:1492</guid><dc:creator>jwooley</dc:creator><slash:comments>2</slash:comments><comments>http://linqinaction.net/blogs/jwooley/comments/1492.aspx</comments><wfw:commentRss>http://linqinaction.net/blogs/jwooley/commentrss.aspx?PostID=1492</wfw:commentRss><wfw:comment>http://linqinaction.net/blogs/jwooley/rsscomments.aspx?PostID=1492</wfw:comment><description>&lt;P&gt;I've uploaded the files for my presentations for the fall Code camp season which I just presented last weekend at the Birmingham, Alabama &lt;A href="http://www.alabamacodecamp.com/"&gt;code camp&lt;/A&gt;. If you missed the talks, you can pick up the downloads at the following links. Also, I will be at the Charleston, &lt;A href="http://www.gcnug.org/Default.aspx?alias=www.gcnug.org/codecamp"&gt;South Carolina code camp&lt;/A&gt; this coming weekend (10/13) so you can catch me there. Additionally, the ThinqLinq talk is still available on the aspnetpodcasts.com. Links to all three parts of thie webcasts are available at &lt;A href="http://www.devauthority.com/blogs/jwooley/archive/2007/07/27/66845.aspx"&gt;http://www.devauthority.com/blogs/jwooley/archive/2007/07/27/66845.aspx&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Below are the links to each of the downloads for my three talks.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.devauthority.com/files/13/jwooley/entry73436.aspx"&gt;Building data driven web sites with Visual Studio 2008 and LINQ&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.devauthority.com/files/13/jwooley/entry84721.aspx"&gt;Language enhancements with VB 9&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.devauthority.com/files/13/jwooley/entry84714.aspx"&gt;What's new with Visual Studio 2008&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;These and all of my demos are available via&amp;nbsp;my&amp;nbsp;downloads page at &lt;A href="http://www.devauthority.com/files/13/jwooley/default.aspx?ppage=1"&gt;http://www.devauthority.com/files/13/jwooley/default.aspx?ppage=1&lt;/A&gt;. Don't let the license notice keep you from downloading the samples. It is just a boilerplate message that I'm not able to change at this point.&lt;/P&gt;
&lt;P&gt;Technorati tags: &lt;A href="http://technorati.com/tag/linq" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=linq"&gt;&lt;FONT color=#444433&gt;linq&lt;/FONT&gt;&lt;/A&gt;, &lt;A href="http://technorati.com/tag/orcas" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=orcas"&gt;&lt;FONT color=#444433&gt;orcas&lt;/FONT&gt;&lt;/A&gt;,&lt;A href="http://technorati.com/tag/vb" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=vb"&gt;&lt;FONT color=#444433&gt;vb&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Crossposted from &lt;A href="http://devauthority.com/blogs/jwooley/default.aspx"&gt;http://devauthority.com/blogs/jwooley/default.aspx&lt;/A&gt;&lt;BR&gt;Updated: 11/9/2007 to fix the invalid hyperlinks due to cross posting.&lt;/P&gt;&lt;img src="http://linqinaction.net/aggbug.aspx?PostID=1492" width="1" height="1"&gt;</description><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/Code+Camp/default.aspx">Code Camp</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/VB/default.aspx">VB</category></item><item><title>LINQ to SQL Compiled Queries</title><link>http://linqinaction.net/blogs/jwooley/archive/2007/09/05/linq-to-sql-compiled-queries.aspx</link><pubDate>Wed, 05 Sep 2007 03:20:00 GMT</pubDate><guid isPermaLink="false">474e9fbe-b00d-4298-b93d-72ccda50e72d:1275</guid><dc:creator>jwooley</dc:creator><slash:comments>6</slash:comments><comments>http://linqinaction.net/blogs/jwooley/comments/1275.aspx</comments><wfw:commentRss>http://linqinaction.net/blogs/jwooley/commentrss.aspx?PostID=1275</wfw:commentRss><wfw:comment>http://linqinaction.net/blogs/jwooley/rsscomments.aspx?PostID=1275</wfw:comment><description>&lt;P&gt;As LINQ nears release, people are starting to consider the performance implications that the extra overhead brings. Currently there are two threads on this: &lt;A href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2085145&amp;amp;SiteID=1"&gt;thread1&lt;/A&gt;, &lt;A href="http://groups.google.com/group/microsoft.public.dotnet.framework.adonet/browse_thread/thread/c4aad76349d13830/e8904f6ba2a02ac5?lnk=st&amp;amp;q=%22LINQ+is+Double+Slower+than+traditional+DATA+Adapter+-+Correct+ME%22&amp;amp;rnum=1&amp;amp;hl=en#e8904f6ba2a02ac5"&gt;thread2&lt;/A&gt;. For those that are intested in the performance implications, I highly recommend checking out the outstanding series of posts by &lt;A href="http://blogs.msdn.com/ricom/archive/tags/performance/default.aspx"&gt;Rico Mariani&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;Ultimately if you want to get the best performance, you need to use the Compile function of the CompiledQuery class. Let's consider the following query (Note, turn this into C# by adding a semi-colon at the end if necessary): &lt;/P&gt;
&lt;P&gt;From&amp;nbsp;p In context.Products() _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Where (p.Price &amp;gt;= minimumPrice) _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Select p)&lt;/P&gt;
&lt;P&gt;In this query, we are searching for the&amp;nbsp;products that are "Expensive" in that their price exceeds a price value that we set. If we regularly consume this, we can eliminate the overhead of building the query pipeline. See the Matt Warren talk about this pipeline on the deep dive video at on &lt;A href="http://blogs.msdn.com/charlie/archive/2007/08/06/linq-to-sql-pipeline-video-with-luca-bolognese-and-matt-warren.aspx"&gt;Charlie Calvert's blog&lt;/A&gt;&amp;nbsp;for more information regarding the overhead that is necessary to evaluate a query.&lt;/P&gt;
&lt;P&gt;To compile the query, we can leverage the CompiledQuery.Compile method. This method takes an argument list as input and a result type. It returns a delegate as a variable that we will be able to consume. Thus in this case, we can pass the DataContext instance and the minimumPrice variable in. We will return a IQueryable(of Product) object. thus we can start the definition of our compiled query as follows:&lt;/P&gt;
&lt;P&gt;VB:&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;CompiledQuery.Compile( _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Function(context As MyDataContext, minimumPrice As Decimal) _&lt;BR&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; From&amp;nbsp;p In context.Products() _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Where (p.Price &amp;gt;= minimumPrice) _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select p)&lt;/P&gt;
&lt;P&gt;C#:&lt;BR&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CompiledQuery.Compile(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (MyDataContext context, decimal minimumPrice) =&amp;gt;&lt;BR&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&amp;nbsp;p in context.Products&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where p.Price &amp;gt;= minimumPrice&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select p);&lt;/P&gt;
&lt;P&gt;In this case we are defining our query as a Lambda function which the Compile can convert to the corresponding expression tree representation. If you are unfamiliar with Lambdas in VB, check out Timothy Ng's recent &lt;A href="http://msdn.microsoft.com/msdnmag/issues/07/09/BasicInstincts/"&gt;MSDN Magazine article&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;With the delegate functions declared, all that is left is to actually assign them to a variable that we can consume later. To do this, we define a static/shared function which returns the Func anonymous delegate type.&amp;nbsp;By&amp;nbsp;defining it as a static/shared function, the compilation will only occur once per AppDomain and will remain cached through the rest of the application's lifetime. Notice, we are defining the signature of the query, not the results. We are free to change the parameter without needing to re-compile the query's structure.&amp;nbsp;Here's the completed function calls in VB and C#:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Public Shared ExpensiveProducts As Func(Of MyDataContext, Decimal, IQueryable(Of Product)) = _&lt;BR&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CompiledQuery.Compile(Function(context As MyDataContext, minimumPrice As Decimal) _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; From&amp;nbsp;p In context.Products() _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Where (p.Price &amp;gt;= minimumPrice) _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Select p)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; public static Func&amp;lt;MyDataContext, decimal, IQueryable&amp;lt;Product&amp;gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ExpensiveProducts =&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/STRONG&gt;&amp;nbsp;CompiledQuery.Compile((MyDataContext context, decimal minimumPrice) =&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&amp;nbsp;p in context.Products&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where p.Price &amp;gt;= minimumPrice&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select p);&lt;/P&gt;
&lt;P&gt;The syntax for using the compiled query takes a bit of thinking to get your head around. We are essentially creating a function which returns a function rather than returning a value. If you're familiar with functional programming such as &lt;A href="http://en.wikipedia.org/wiki/OCaml"&gt;OCaml&lt;/A&gt; and F#, this concept should be easy to grok. For the rest of us, piece out the individual method components as laid out above and you should be able to understand it after a while ;-)&lt;/P&gt;
&lt;P&gt;To consume our new function, we simply instantiate a DataContext instance and pass it along with the minimumPrice we want to set as our minimum value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;Dim context As New MyContext(My.Settings.MyConnectionString)&lt;BR&gt;&amp;nbsp;&amp;nbsp;Dim Items = ExpensiveProducts(context, minimumPrice)&lt;/P&gt;
&lt;P&gt;Technorati tags: &lt;A href="http://technorati.com/tag/linq" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=linq"&gt;&lt;FONT color=#444433&gt;linq&lt;/FONT&gt;&lt;/A&gt;, &lt;A href="http://technorati.com/tag/orcas" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=orcas"&gt;&lt;FONT color=#444433&gt;orcas&lt;/FONT&gt;&lt;/A&gt;,&lt;A href="http://technorati.com/tag/vb" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=vb"&gt;&lt;FONT color=#444433&gt;vb&lt;/FONT&gt;&lt;/A&gt;, &lt;A href="http://technorati.com/tag/c#" rel=tag&gt;&lt;IMG alt=" " src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=c#"&gt;&lt;FONT color=#444433&gt;C#&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
Crossposted from http://devauthority.com/blogs/jwooley/default.aspx&lt;img src="http://linqinaction.net/aggbug.aspx?PostID=1275" width="1" height="1"&gt;</description><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://linqinaction.net/blogs/jwooley/archive/tags/VB/default.aspx">VB</category></item></channel></rss>