<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Andrew Hay - LINQ</title>
    <link>http://www.a7drew.com/blog/</link>
    <description>Thinking way too long about the subtitle</description>
    <language>en-us</language>
    <copyright>Andrew Hay</copyright>
    <lastBuildDate>Wed, 28 Jan 2009 02:57:42 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>andrewcameronhay@hotmail.com</managingEditor>
    <webMaster>andrewcameronhay@hotmail.com</webMaster>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=b191edc1-c409-4467-978e-ba58adab6d1d</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,b191edc1-c409-4467-978e-ba58adab6d1d.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,b191edc1-c409-4467-978e-ba58adab6d1d.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=b191edc1-c409-4467-978e-ba58adab6d1d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When I've needed to invoke a cache mechanism for data on a web site, I've happily
used the ASP.Net Cache object over the past few years. It's a nice model for storing
a chunk of data under a key until a specific time or for a given duration, providing
the application domain doesn't recycle.
</p>
        <p>
I've even hooked up some systems that resemble a Rube Goldberg apparatus to get the
same features as a job scheduler, but I don't like to talk about them. ASP.Net will
fire an event that you can use when something expires from the cache and therefore
give you the "Cache, Wait, Expire, Do Something, Repeat" model for solving
a job scheduling problem.
</p>
        <p>
.Net 2.0 and up supported the <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx">SqlCacheDependency</a> class.
One of its talents is the ability to expire a cached object when the underlying database
changes. Before this feature was available, you might wire up specific cache expiration
logic in relevant insert, update or delete functions of your app. However, that falls
short when the database is updated outside of the trip wires you set in your own code.
Another application might update that database, or someone might edit the database
through Query Analyzer.
</p>
        <p>
Way back in the day, my team set out to bag the big beast by writing a trigger in
SQL Server that would reach out an edit a text file whenever a specific table was
modified. This event would be observed by the cache dependency object that was good
at watching physical files for changes and ska-doosh! We've got our SQL Server cache
dependency working.
</p>
        <p>
Nowadays, those long, verbose methods aren't required. We've got a set of fancy new
tools, namely, the SqlCacheDependency object. Or so I thought.
</p>
        <p>
Its great that support goes all the way back to SQL Server 7, albeit via a costly
polling mechanism. SQL Server 2005 gave us a direct feedback loop that avoided the
polling system. I've know about this feature, as a concept, since it came out but
only in the past few weeks have I really looked at the code to use it and I'm honestly
a little shocked at the site. 
</p>
        <p>
I was really expecting something akin to the familiar practice of checking the cache
for the existence of the object, and if necessary, repopulation of the cache. It turns
out there's a few more steps, and a little craziness if you want to use LINQ to access
your database. 
</p>
        <p>
I'll always leave the door open and hope that some day I'll learn the super easy way
of doing this, but today, so much of this is in the non-trivial bucket that I'm really
tempted to leave it alone and continue my current caching practices.
</p>
        <p>
A year or more ago, I would reach for the data access block inside the <a href="http://www.codeplex.com/entlib">Enterprise
Library</a> as a default choice. It's a great data access layer and sure beats writing
all that goo on my own. Today, I'll reach for LINQ as my default choice. As I understand
it, Microsoft is touting the Entity Framework as the future of data access, but today,
I'm still sitting contently in the LINQ boat. Who knows what the future holds for
me.
</p>
        <p>
To be fair, the example code in MSDN that shows how to implement the SqlCacheDependency
actually looks pretty good when you're creating your own Command objects. Not much
to talk about there. But I was disappointed when I looked at the LINQ story. Perhaps
LINQ just got out ahead too far from the pack and better things are coming. 
</p>
        <p>
          <a href="http://code.msdn.microsoft.com/linqtosqlcache">One of the cleaner approaches
to caching LINQ data</a> I've seen was on the MSDN Code Gallery web site. Here, the
developer created a sweet extension method that creates the Command object on the
fly, executes the query and caches it all inside a generic method. It bears a little
more review on my part, but its neat enough that I thought I'd share it. Its less
than 50 lines of actual code, but they comment the crap out of it.
</p>
        <p>
The whole idea of having SQL Server notify ASP.Net involves more considerations than
the simplistic approach that served me well for years. For example, I can have SQL
Server watch a specific table, a set of tables, or a specific query (this last options
requires SQL Server 2005 or better). In addition, you'll need to start and stop the
entire process when the application domain comes up and gets recycled respectively.
You probably with a few lines of code in Global.asax. Finally, you're going to need
a few more elements inside your config file that describe a few more details of the
cache behavior. All in all, I like the direction, but I'll be happier when/if the
LINQ story improves.
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=b191edc1-c409-4467-978e-ba58adab6d1d" />
      </body>
      <title>LINQ and the SqlCacheDependency</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,b191edc1-c409-4467-978e-ba58adab6d1d.aspx</guid>
      <link>http://www.a7drew.com/blog/2009/01/28/LINQAndTheSqlCacheDependency.aspx</link>
      <pubDate>Wed, 28 Jan 2009 02:57:42 GMT</pubDate>
      <description>&lt;p&gt;
When I've needed to invoke a cache mechanism for data on a web site, I've happily
used the ASP.Net Cache object over the past few years. It's a nice model for storing
a chunk of data under a key until a specific time or for a given duration, providing
the application domain doesn't recycle.
&lt;/p&gt;
&lt;p&gt;
I've even hooked up some systems that resemble a Rube Goldberg apparatus to get the
same features as a job scheduler, but I don't like to talk about them. ASP.Net will
fire an event that you can use when something expires from the cache and therefore
give you the &amp;quot;Cache, Wait, Expire, Do Something, Repeat&amp;quot; model for solving
a job scheduling problem.
&lt;/p&gt;
&lt;p&gt;
.Net 2.0 and up supported the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx"&gt;SqlCacheDependency&lt;/a&gt; class.
One of its talents is the ability to expire a cached object when the underlying database
changes. Before this feature was available, you might wire up specific cache expiration
logic in relevant insert, update or delete functions of your app. However, that falls
short when the database is updated outside of the trip wires you set in your own code.
Another application might update that database, or someone might edit the database
through Query Analyzer.
&lt;/p&gt;
&lt;p&gt;
Way back in the day, my team set out to bag the big beast by writing a trigger in
SQL Server that would reach out an edit a text file whenever a specific table was
modified. This event would be observed by the cache dependency object that was good
at watching physical files for changes and ska-doosh! We've got our SQL Server cache
dependency working.
&lt;/p&gt;
&lt;p&gt;
Nowadays, those long, verbose methods aren't required. We've got a set of fancy new
tools, namely, the SqlCacheDependency object. Or so I thought.
&lt;/p&gt;
&lt;p&gt;
Its great that support goes all the way back to SQL Server 7, albeit via a costly
polling mechanism. SQL Server 2005 gave us a direct feedback loop that avoided the
polling system. I've know about this feature, as a concept, since it came out but
only in the past few weeks have I really looked at the code to use it and I'm honestly
a little shocked at the site. 
&lt;/p&gt;
&lt;p&gt;
I was really expecting something akin to the familiar practice of checking the cache
for the existence of the object, and if necessary, repopulation of the cache. It turns
out there's a few more steps, and a little craziness if you want to use LINQ to access
your database. 
&lt;/p&gt;
&lt;p&gt;
I'll always leave the door open and hope that some day I'll learn the super easy way
of doing this, but today, so much of this is in the non-trivial bucket that I'm really
tempted to leave it alone and continue my current caching practices.
&lt;/p&gt;
&lt;p&gt;
A year or more ago, I would reach for the data access block inside the &lt;a href="http://www.codeplex.com/entlib"&gt;Enterprise
Library&lt;/a&gt; as a default choice. It's a great data access layer and sure beats writing
all that goo on my own. Today, I'll reach for LINQ as my default choice. As I understand
it, Microsoft is touting the Entity Framework as the future of data access, but today,
I'm still sitting contently in the LINQ boat. Who knows what the future holds for
me.
&lt;/p&gt;
&lt;p&gt;
To be fair, the example code in MSDN that shows how to implement the SqlCacheDependency
actually looks pretty good when you're creating your own Command objects. Not much
to talk about there. But I was disappointed when I looked at the LINQ story. Perhaps
LINQ just got out ahead too far from the pack and better things are coming. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://code.msdn.microsoft.com/linqtosqlcache"&gt;One of the cleaner approaches
to caching LINQ data&lt;/a&gt; I've seen was on the MSDN Code Gallery web site. Here, the
developer created a sweet extension method that creates the Command object on the
fly, executes the query and caches it all inside a generic method. It bears a little
more review on my part, but its neat enough that I thought I'd share it. Its less
than 50 lines of actual code, but they comment the crap out of it.
&lt;/p&gt;
&lt;p&gt;
The whole idea of having SQL Server notify ASP.Net involves more considerations than
the simplistic approach that served me well for years. For example, I can have SQL
Server watch a specific table, a set of tables, or a specific query (this last options
requires SQL Server 2005 or better). In addition, you'll need to start and stop the
entire process when the application domain comes up and gets recycled respectively.
You probably with a few lines of code in Global.asax. Finally, you're going to need
a few more elements inside your config file that describe a few more details of the
cache behavior. All in all, I like the direction, but I'll be happier when/if the
LINQ story improves.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=b191edc1-c409-4467-978e-ba58adab6d1d" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,b191edc1-c409-4467-978e-ba58adab6d1d.aspx</comments>
      <category>asp.net</category>
      <category>LINQ</category>
    </item>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=1a932d00-9118-4880-836d-1f88e95f4bec</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,1a932d00-9118-4880-836d-1f88e95f4bec.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,1a932d00-9118-4880-836d-1f88e95f4bec.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=1a932d00-9118-4880-836d-1f88e95f4bec</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just had a Pwop moment (<a href="http://franklins.net/carl.aspx">the sound of a
forehead slap</a>) with LINQ:
</p>
        <p>
I needed to retrieve a distinct list of years contained in a collection of objects.
The data was in the form of an generic List&lt;T&gt;, already loaded into memory.
So without thinking too much about it, I sat down and wrote a quick LINQ expression
that included the Distinct() operator and since the years were actually properties
on the objects, I wrote my own implementation of IEqualityComparer. 
</p>
        <p>
It had some problems because the resulting data was not really all that distinct,
and then it happened: PWOP!
</p>
        <p>
I don't need to implement an interface. I can simply use the default equality comparer
for a List&lt;int&gt; like so.
</p>
        <p>
          <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="177" alt="LINQ Distinct() Operator" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/PwopMomentswithLINQ_9902/GetDistinctYearList_3.png" width="390" border="0" />
        </p>
        <p>
Gah!
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=1a932d00-9118-4880-836d-1f88e95f4bec" />
      </body>
      <title>Pwop Moments with LINQ</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,1a932d00-9118-4880-836d-1f88e95f4bec.aspx</guid>
      <link>http://www.a7drew.com/blog/2008/07/31/PwopMomentsWithLINQ.aspx</link>
      <pubDate>Thu, 31 Jul 2008 17:52:54 GMT</pubDate>
      <description>&lt;p&gt;
I just had a Pwop moment (&lt;a href="http://franklins.net/carl.aspx"&gt;the sound of a
forehead slap&lt;/a&gt;) with LINQ:
&lt;/p&gt;
&lt;p&gt;
I needed to retrieve a distinct list of years contained in a collection of objects.
The data was in the form of an generic List&amp;lt;T&amp;gt;, already loaded into memory.
So without thinking too much about it, I sat down and wrote a quick LINQ expression
that included the Distinct() operator and since the years were actually properties
on the objects, I wrote my own implementation of IEqualityComparer. 
&lt;/p&gt;
&lt;p&gt;
It had some problems because the resulting data was not really all that distinct,
and then it happened: PWOP!
&lt;/p&gt;
&lt;p&gt;
I don't need to implement an interface. I can simply use the default equality comparer
for a List&amp;lt;int&amp;gt; like so.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="177" alt="LINQ Distinct() Operator" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/PwopMomentswithLINQ_9902/GetDistinctYearList_3.png" width="390" border="0" /&gt;
&lt;/p&gt;
&lt;p&gt;
Gah!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=1a932d00-9118-4880-836d-1f88e95f4bec" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,1a932d00-9118-4880-836d-1f88e95f4bec.aspx</comments>
      <category>LINQ</category>
    </item>
    <item>
      <trackback:ping>http://www.a7drew.com/blog/Trackback.aspx?guid=e56335bf-89d5-4cac-900c-73583fb5dba7</trackback:ping>
      <pingback:server>http://www.a7drew.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.a7drew.com/blog/PermaLink,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</pingback:target>
      <dc:creator>Andrew Hay</dc:creator>
      <wfw:comment>http://www.a7drew.com/blog/CommentView,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</wfw:comment>
      <wfw:commentRss>http://www.a7drew.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=e56335bf-89d5-4cac-900c-73583fb5dba7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
LINQ is an acronym for Language-Integrated Query and a new feature in v3.5 of the
.Net Framework from Microsoft. This new version of Microsoft .Net reached RTM status
a couple of weeks ago — this framework is chock full of brilliant things I can
use to improve my efficiency and effectiveness on daily tasks here at Pop Art.
</p>
        <p>
As one of my favorite features, LINQ helps me write data-driven application even faster
than what .Net 2.0 brought to the table. A common example starts with launching Visual
Studio 2008. After dragging a collection of tables from a SQL Server database onto
a surface in my solution, I can see a visualization of the columns in the tables as
well as the relationships between them. Click the following thumbnail to see a larger
image.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="196" alt="database schema" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
Next, I start writing data access code directly in my C# program as opposed to switching
languages and writing in the T-SQL language. Visual Studio gives me Intellisense here
too; as I type the name of a table and click the period key, all of the columns in
the table appear where the cursor is located. Big time saver. Huge! I'm certain that
I didn't misspell a column name and that my code will compile.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="192" alt="intellisense is beautiful" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
The syntax for LINQ in a C# program is very similar to the T-SQL language, which is
a "set based" language. LINQ statements are compiled, just like the rest
of my C# code. The first thing someone well versed in T-SQL will notice is that the
columns normally specified in a SELECT query are at the end of the LINQ statement
instead of the start as in T-SQL. The idea is that you're articulating your constraints
at the start of the LINQ statement and then pulling out the fields you need at the
very end.
</p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="169" alt="a LINQ sample" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
Behind the scenes, LINQ is using the relationships expressed in the database to generate
T-SQL scripts on the fly. This is a clear line of demarcation for LINQ. If you're
using stored procedures exclusively for database access, then LINQ isn't going to
buy you much. You'll still get Intellisense inside Visual Studio and you can specify
an existing stored procedure instead of using the auto-generated SQL, but you're giving
up a lot of acceleration tools. Perhaps more than you're getting in return.
</p>
        <p>
LINQ really shines in multiple table joins and aggregation. The following two blocks
of code show a query is performed in LINQ and its equivalent T-SQL script. The query
retrieves product information from three tables where the list price is below a given
amount and a sub-category name exists. If you go the T-SQL route, you will still need
to write some C# code to call your database query.
</p>
        <p>
Which of the following versions would you rather author and support?
</p>
        <p>
          <strong>LINQ Version</strong>
        </p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_2.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="201" alt="a more complex LINQ statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_thumb.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
          <strong>T-SQL Version</strong>
        </p>
        <p>
          <a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_4.jpg" target="_blank">
            <img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="an equivalent T-SQL statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_thumb_1.jpg" width="244" border="0" />
          </a>
        </p>
        <p>
MSDN is a great online resource for developers and they really hit a home-run here.
They have a page with <a href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx" target="_blank">101
LINQ samples</a>. This is my preferred way to learn when I already know the surrounding
technologies and I want to fill in a specific gap. The page categorizes several ways
of retrieving and iterating over information. 
</p>
        <img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=e56335bf-89d5-4cac-900c-73583fb5dba7" />
      </body>
      <title>Understanding LINQ</title>
      <guid isPermaLink="false">http://www.a7drew.com/blog/PermaLink,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</guid>
      <link>http://www.a7drew.com/blog/2007/12/19/UnderstandingLINQ.aspx</link>
      <pubDate>Wed, 19 Dec 2007 01:36:23 GMT</pubDate>
      <description>&lt;p&gt;
LINQ is an acronym for Language-Integrated Query and a new feature in v3.5 of the
.Net Framework from Microsoft. This new version of Microsoft .Net reached RTM status
a couple of weeks ago &amp;#8212; this framework is chock full of brilliant things I can
use to improve my efficiency and effectiveness on daily tasks here at Pop Art.
&lt;/p&gt;
&lt;p&gt;
As one of my favorite features, LINQ helps me write data-driven application even faster
than what .Net 2.0 brought to the table. A common example starts with launching Visual
Studio 2008. After dragging a collection of tables from a SQL Server database onto
a surface in my solution, I can see a visualization of the columns in the tables as
well as the relationships between them. Click the following thumbnail to see a larger
image.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="196" alt="database schema" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-map_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Next, I start writing data access code directly in my C# program as opposed to switching
languages and writing in the T-SQL language. Visual Studio gives me Intellisense here
too; as I type the name of a table and click the period key, all of the columns in
the table appear where the cursor is located. Big time saver. Huge! I'm certain that
I didn't misspell a column name and that my code will compile.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="192" alt="intellisense is beautiful" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-intellisense_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The syntax for LINQ in a C# program is very similar to the T-SQL language, which is
a &amp;quot;set based&amp;quot; language. LINQ statements are compiled, just like the rest
of my C# code. The first thing someone well versed in T-SQL will notice is that the
columns normally specified in a SELECT query are at the end of the LINQ statement
instead of the start as in T-SQL. The idea is that you're articulating your constraints
at the start of the LINQ statement and then pulling out the fields you need at the
very end.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="169" alt="a LINQ sample" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sample_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Behind the scenes, LINQ is using the relationships expressed in the database to generate
T-SQL scripts on the fly. This is a clear line of demarcation for LINQ. If you're
using stored procedures exclusively for database access, then LINQ isn't going to
buy you much. You'll still get Intellisense inside Visual Studio and you can specify
an existing stored procedure instead of using the auto-generated SQL, but you're giving
up a lot of acceleration tools. Perhaps more than you're getting in return.
&lt;/p&gt;
&lt;p&gt;
LINQ really shines in multiple table joins and aggregation. The following two blocks
of code show a query is performed in LINQ and its equivalent T-SQL script. The query
retrieves product information from three tables where the list price is below a given
amount and a sub-category name exists. If you go the T-SQL route, you will still need
to write some C# code to call your database query.
&lt;/p&gt;
&lt;p&gt;
Which of the following versions would you rather author and support?
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;LINQ Version&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_2.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="201" alt="a more complex LINQ statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-complex_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;T-SQL Version&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_4.jpg" target="_blank"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="an equivalent T-SQL statement" src="http://www.andrewdothay.net/blog/content/binary/WindowsLiveWriter/UnderstandingLINQ_8EA2/linq-sql_thumb_1.jpg" width="244" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
MSDN is a great online resource for developers and they really hit a home-run here.
They have a page with &lt;a href="http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx" target="_blank"&gt;101
LINQ samples&lt;/a&gt;. This is my preferred way to learn when I already know the surrounding
technologies and I want to fill in a specific gap. The page categorizes several ways
of retrieving and iterating over information. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.a7drew.com/blog/aggbug.ashx?id=e56335bf-89d5-4cac-900c-73583fb5dba7" /&gt;</description>
      <comments>http://www.a7drew.com/blog/CommentView,guid,e56335bf-89d5-4cac-900c-73583fb5dba7.aspx</comments>
      <category>LINQ</category>
      <category>SQL Server</category>
      <category>Visual Studio</category>
    </item>
  </channel>
</rss>