<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C.M. Jackson .Net &#187; Linq To SQL</title>
	<atom:link href="http://www.cmjackson.net/tag/linq-to-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cmjackson.net</link>
	<description>Web Design, Programming, Tutorials</description>
	<lastBuildDate>Tue, 18 May 2010 22:43:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Selecting the last item of a collection using Linq-to-sql</title>
		<link>http://www.cmjackson.net/2010/01/29/selecting-the-last-item-of-a-collection-using-linq-to-sql/</link>
		<comments>http://www.cmjackson.net/2010/01/29/selecting-the-last-item-of-a-collection-using-linq-to-sql/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 14:00:00 +0000</pubDate>
		<dc:creator>Chris Jackson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Linq To SQL]]></category>

		<guid isPermaLink="false">http://www.cmjackson.net/?p=367</guid>
		<description><![CDATA[Today I had some problems with Linq-To-SQL. I needed to get the last item from a collection and only return items where the date was older than two weeks from now. Data Tables The main items that I want to return from Linq are Revision records. A Revision has a collection of Document_History records that [...]]]></description>
			<content:encoded><![CDATA[<p>Today I had some problems with Linq-To-SQL. I needed to get the last item from a collection and only return items where the date was older than two weeks from now.</p>
<p><strong>Data Tables</strong><br />
The main items that I want to return from Linq are Revision records. A Revision has a collection of Document_History records that keep track of updates to the revision. The Document_History record has a Date field that is the date and time when the history entry was inserted, or when the revision was modified.</p>
<p><strong>The Problem</strong><br />
When using the data objects that Linq generates, you are allowed to perform some functions, such as Last and Reverse on the collection of items.</p>
<pre class="brush: plain;">Revision.Document_History.Last.Date</pre>
<p>The above code would return the most recent date, or the last time a revision was updated.</p>
<p>But, when you try to use the Last method within a Linq statement, Visual Studio throws an error.</p>
<p><strong>The Fix</strong><br />
To get around this error, I figured out that you can select the latest date using a sub query and the Max method. Below is an example of the Linq-To-SQL code that worked for me.</p>
<pre class="brush: plain;">
Dim revs As List(Of Revision) = From r In dbcontext.Revisions _
                      Where (From h In r.Document_History _
                             Select h.Date).Max &lt;= cutoffDate _
                             Select r
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cmjackson.net/2010/01/29/selecting-the-last-item-of-a-collection-using-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
