<?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>SquaredRoot &#187; LINQ</title>
	<atom:link href="http://www.squaredroot.com/category/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.squaredroot.com</link>
	<description>.Net Development in DC</description>
	<lastBuildDate>Sun, 16 Aug 2009 01:30:15 +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>Return of the PagedList</title>
		<link>http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/</link>
		<comments>http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 10:00:40 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[CodePlex]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[paging]]></category>

		<guid isPermaLink="false">http://www.squaredroot.com/?p=462</guid>
		<description><![CDATA[A few days ago, Craig Stuntz reported an interesting observation: when the first page is returned, the class performs a Skip(0). Suprisingly, this is not free. With that in mind, I set out to correct that issue as well as incorporate a few changes I've made over the past year. The result is nearly identical to the last posted version, just a bit more readable. Additionally...]]></description>
			<content:encoded><![CDATA[<p>It has been nearly a year since I <a href="http://www.squaredroot.com/2008/07/08/PagedList-Strikes-Back/">posted</a> an updated version of the PagedList&lt;T&gt; functionality originally <a href="http://blog.wekeroad.com/blog/aspnet-mvc-pagedlistt">created by Scott Guthrie and posted by Rob Conery</a>. Since then I have used the class in a number of projects and find it indispensable.</p>
<p>A few days ago, Craig Stuntz reported an interesting observation: when the first page is returned, the class performs a Skip(0). Suprisingly, <a href="http://blogs.teamb.com/craigstuntz/2009/06/10/38313/">this is not free</a>. With that in mind, I set out to correct that issue as well as incorporate a few changes I&#8217;ve made over the past year. The result is nearly identical to the last posted version, just a bit more readable. Additionally&#8230;<br />
<span id="more-462"></span></p>
<ul>
<li>The source is now available on CodePlex: <a href="http://pagedlist.codeplex.com">http://pagedlist.codeplex.com</a>. This should make finding and downloading the code easier than finding the correct blog entry on some dude&#8217;s blog.</li>
<li>I have posted a release-compiled, XML commented, signed assembly on CodePlex. I got tired of having to copy the source into multiple projects and finding a place to put it in that project&#8217;s taxonomy.</li>
<li>Further incremental changes can be found in the Change Log on the CodePlex project site.</li>
</ul>
<h3><a href="http://pagedlist.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=28585#ReleaseFiles">Download from CodePlex</a></h3>
<p></p>
<h4>IPagedList&lt;T&gt;.cs</h4>
<pre class="brush: csharp">using System.Collections.Generic;

namespace PagedList
{
	public interface IPagedList&lt;T&gt; : IList&lt;T&gt;
	{
		int PageCount { get; }
		int TotalItemCount { get; }
		int PageIndex { get; }
		int PageNumber { get; }
		int PageSize { get; }
		bool HasPreviousPage { get; }
		bool HasNextPage { get; }
		bool IsFirstPage { get; }
		bool IsLastPage { get; }
	}
}</pre>
<h4>PagedList&lt;T&gt;.cs</h4>
<pre class="brush: csharp">using System;
using System.Collections.Generic;
using System.Linq;

namespace PagedList
{
	public class PagedList&lt;T&gt; : List&lt;T&gt;, IPagedList&lt;T&gt;
	{
		public PagedList(IEnumerable&lt;T&gt; superset, int index, int pageSize)
		{
			// set source to blank list if superset is null to prevent exceptions
			var source = superset == null
			                      	? new List&lt;T&gt;().AsQueryable()
									: superset.AsQueryable();

			TotalItemCount = source.Count();
			PageSize = pageSize;
			PageIndex = index;
			if (TotalItemCount &gt; 0)
				PageCount = (int) Math.Ceiling(TotalItemCount/(double) PageSize);
			else
				PageCount = 0;

			if (index &lt; 0)
				throw new ArgumentOutOfRangeException("index", index, "PageIndex cannot be below 0.");
			if (pageSize &lt; 1)
				throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");

			// add items to internal list
			if (TotalItemCount &gt; 0)
				if (index == 0)
					AddRange(source.Take(pageSize).ToList());
				else
					AddRange(source.Skip((index) * pageSize).Take(pageSize).ToList());
		}

		public int PageCount { get; private set; }
		public int TotalItemCount { get; private set; }
		public int PageIndex { get; private set; }
		public int PageSize { get; private set; }

		public int PageNumber
		{
			get { return PageIndex + 1; }
		}

		public bool HasPreviousPage
		{
			get { return PageIndex &gt; 0; }
		}

		public bool HasNextPage
		{
			get { return PageIndex &lt; (PageCount - 1); }
		}

		public bool IsFirstPage
		{
			get { return PageIndex &lt;= 0; }
		}

		public bool IsLastPage
		{
			get { return PageIndex &gt;= (PageCount - 1); }
		}
	}
}</pre>
<h4>PagedListExtensions.cs</h4>
<pre class="brush: csharp">using System.Collections.Generic;
using System.Linq;

namespace PagedList
{
	public static class PagedListExtensions
	{
		public static IPagedList&lt;T&gt; ToPagedList&lt;T&gt;(this IEnumerable&lt;T&gt; superset, int index, int pageSize)
		{
			return new PagedList&lt;T&gt;(superset, index, pageSize);
		}
	}
}</pre>
<h4>PagedListFacts.cs</h4>
<pre class="brush: csharp; collapse: true">using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Extensions;

namespace PagedList.Tests
{
	public class PagedListFacts
	{
		[Fact]
		public void Null_Data_Set_Doesnt_Throw_Exception()
		{
			//act
			Assert.ThrowsDelegate act = () =&gt; new PagedList&lt;object&gt;(null, 0, 10);

			//assert
			Assert.DoesNotThrow(act);
		}

		[Fact]
		public void PageIndex_Below_Zero_Throws_ArgumentOutOfRange()
		{
			//arrange
			var data = new[] {1, 2, 3};

			//act
			Assert.ThrowsDelegate act = () =&gt; data.ToPagedList(-1, 1);

			//assert
			Assert.Throws&lt;ArgumentOutOfRangeException&gt;(act);
		}

		[Fact]
		public void PageIndex_Above_RecordCount_Returns_Empty_List()
		{
			//arrange
			var data = new[] {1, 2, 3};

			//act
			var pagedList = data.ToPagedList(2, 3);

			//assert
			Assert.Equal(0, pagedList.Count);
		}

		[Fact]
		public void PageSize_Below_One_Throws_ArgumentOutOfRange()
		{
			//arrange
			var data = new[] {1, 2, 3};

			//act
			Assert.ThrowsDelegate act = () =&gt; data.ToPagedList(0, 0);

			//assert
			Assert.Throws&lt;ArgumentOutOfRangeException&gt;(act);
		}

		[Fact]
		public void Null_Data_Set_Doesnt_Return_Null()
		{
			//act
			var pagedList = new PagedList&lt;object&gt;(null, 0, 10);

			//assert
			Assert.NotNull(pagedList);
		}

		[Fact]
		public void Null_Data_Set_Returns_Zero_Pages()
		{
			//act
			var pagedList = new PagedList&lt;object&gt;(null, 0, 10);

			//assert
			Assert.Equal(0, pagedList.PageCount);
		}

		[Fact]
		public void Zero_Item_Data_Set_Returns_Zero_Pages()
		{
			//arrange
			var data = new List&lt;object&gt;();

			//act
			var pagedList = data.ToPagedList(0, 10);

			//assert
			Assert.Equal(0, pagedList.PageCount);
		}

		[Fact]
		public void DataSet_Of_One_Through_Five_PageSize_Of_Two_PageIndex_Of_One_First_Item_Is_Three()
		{
			//arrange
			var data = new[] {1, 2, 3, 4, 5};

			//act
			var pagedList = data.ToPagedList(1, 2);

			//assert
			Assert.Equal(3, pagedList[0]);
		}

		[Fact]
		public void TotalCount_Is_Preserved()
		{
			//arrange
			var data = new[] {1, 2, 3, 4, 5};

			//act
			var pagedList = data.ToPagedList(1, 2);

			//assert
			Assert.Equal(5, pagedList.TotalItemCount);
		}

		[Fact]
		public void PageIndex_Is_Preserved()
		{
			//arrange
			var data = new[] {1, 2, 3, 4, 5};

			//act
			var pagedList = data.ToPagedList(1, 2);

			//assert
			Assert.Equal(1, pagedList.PageIndex);
		}

		[Fact]
		public void PageSize_Is_Preserved()
		{
			//arrange
			var data = new[] {1, 2, 3, 4, 5};

			//act
			var pagedList = data.ToPagedList(1, 2);

			//assert
			Assert.Equal(2, pagedList.PageSize);
		}

		[Fact]
		public void Data_Is_Filtered_By_PageSize()
		{
			//arrange
			var data = new[] {1, 2, 3, 4, 5};

			//act
			var pagedList = data.ToPagedList(1, 2);

			//assert
			Assert.Equal(2, pagedList.Count);

			//### related test below

			//act
			pagedList = data.ToPagedList(2, 2);

			//assert
			Assert.Equal(1, pagedList.Count);
		}

		[Fact]
		public void DataSet_OneThroughSix_PageSize_Three_PageIndex_Zero_FirstValue_Is_One()
		{
			//arrange
			var data = new[] { 1, 2, 3, 4, 5, 6 };

			//act
			var pagedList = data.ToPagedList(0, 3);

			//assert
			Assert.Equal(1, pagedList[0]);
		}

		[Fact]
		public void DataSet_OneThroughThree_PageSize_One_PageIndex_Two_HasNextPage_False()
		{
			//arrange
			var data = new[] {1, 2, 3};

			//act
			var pagedList = data.ToPagedList(2, 1);

			//assert
			Assert.Equal(false, pagedList.HasNextPage);
		}

		[Fact]
		public void DataSet_OneThroughThree_PageSize_One_PageIndex_Two_IsLastPage_True()
		{
			//arrange
			var data = new[] {1, 2, 3};

			//act
			var pagedList = data.ToPagedList(2, 1);

			//assert
			Assert.Equal(true, pagedList.IsLastPage);
		}

		[Fact]
		public void DataSet_OneAndTwo_PageSize_One_PageIndex_One_FirstValue_Is_Two()
		{
			//arrange
			var data = new[] { 1, 2 };

			//act
			var pagedList = data.ToPagedList(1, 1);

			//assert
			Assert.Equal(2, pagedList[0]);
		}

		[Theory]
		[InlineData(new[] {1, 2, 3}, 0, 1)]
		[InlineData(new[] {1, 2, 3}, 1, 2)]
		[InlineData(new[] {1, 2, 3}, 2, 3)]
		public void Theory_PageNumber_Is_PageIndex_Plus_One(int[] integers, int pageIndex, int expectedPageNumber)
		{
			//arrange
			var data = integers;

			//act
			var pagedList = data.ToPagedList(pageIndex, 1);

			//assert
			Assert.Equal(expectedPageNumber, pagedList.PageNumber);
		}

		[Theory]
		[InlineData(new[] {1, 2, 3}, 0, 1, false, true)]
		[InlineData(new[] {1, 2, 3}, 1, 1, true, true)]
		[InlineData(new[] {1, 2, 3}, 2, 1, true, false)]
		public void Theory_HasPreviousPage_And_HasNextPage_Are_Correct(int[] integers, int pageIndex, int pageSize,
		                                                               bool expectedHasPrevious, bool expectedHasNext)
		{
			//arrange
			var data = integers;

			//act
			var pagedList = data.ToPagedList(pageIndex, pageSize);

			//assert
			Assert.Equal(expectedHasPrevious, pagedList.HasPreviousPage);
			Assert.Equal(expectedHasNext, pagedList.HasNextPage);
		}

		[Theory]
		[InlineData(new[] {1, 2, 3}, 0, 1, true, false)]
		[InlineData(new[] {1, 2, 3}, 1, 1, false, false)]
		[InlineData(new[] {1, 2, 3}, 2, 1, false, true)]
		public void Theory_IsFirstPage_And_IsLastPage_Are_Correct(int[] integers, int pageIndex, int pageSize,
		                                                          bool expectedIsFirstPage, bool expectedIsLastPage)
		{
			//arrange
			var data = integers;

			//act
			var pagedList = data.ToPagedList(pageIndex, pageSize);

			//assert
			Assert.Equal(expectedIsFirstPage, pagedList.IsFirstPage);
			Assert.Equal(expectedIsLastPage, pagedList.IsLastPage);
		}

		[Theory]
		[InlineData(new[] {1, 2, 3}, 1, 3)]
		[InlineData(new[] {1, 2, 3}, 3, 1)]
		[InlineData(new[] {1}, 1, 1)]
		[InlineData(new[] {1, 2, 3}, 2, 2)]
		[InlineData(new[] {1, 2, 3, 4}, 2, 2)]
		[InlineData(new[] {1, 2, 3, 4, 5}, 2, 3)]
		public void Theory_PageCount_Is_Correct(int[] integers, int pageSize, int expectedNumberOfPages)
		{
			//arrange
			var data = integers;

			//act
			var pagedList = data.ToPagedList(0, pageSize);

			//assert
			Assert.Equal(expectedNumberOfPages, pagedList.PageCount);
		}
	}
}</pre>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2009/06/15/return-of-the-pagedlist/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Storing LINQ Objects in SQL-Based Session State</title>
		<link>http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/</link>
		<comments>http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 01:19:00 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">/post/2008/01/30/Storing-LINQ-Objects-in-SQL-Based-Session-State.aspx</guid>
		<description><![CDATA[Scott Hanselman recently posted about various options you have for session storage while using ASP.Net. In the comments of his post I brough up an issue I recently encountered at work (where we use SQL Server session state): LINQ-To-Sql generated objects are not marked [Serializable] and cannot be stored in out-of-process session storage. To get [...]]]></description>
			<content:encoded><![CDATA[<p>
Scott Hanselman <a href="/post/2008/01/Storing-LINQ-Objects-in-SQL-Based-Session-State.aspx">recently posted about various options you have for session storage</a> while using ASP.Net. In the comments of his post I brough up an issue I recently encountered at work (where we use SQL Server session state):
</p>
<p>
<strong>LINQ-To-Sql generated objects are not marked [Serializable] and cannot be stored in out-of-process session storage.</strong>
</p>
<p>
To get around this I have whipped up the following helper class which will serialize LINQ-To-Sql objects if you set the DataContext&#39;s Serializable property to &quot;Unidirectional&quot;.
</p>
<p>
<strong>Note: The following class does not current work for storing List&lt;X&gt; where X is a LINQ-To-Sql object. I&#39;ll be working to resolve that sometime later this week.</strong>
</p>
<div class="csharpcode-wrapper">
<div class="csharpcode">
<pre class="alt">
<span class="lnum">   1:</span> <span class="kwrd">using</span> System;
</pre>
<pre class="alteven">
<span class="lnum">   2:</span> <span class="kwrd">using</span> System.IO;
</pre>
<pre class="alt">
<span class="lnum">   3:</span> <span class="kwrd">using</span> System.Runtime.Serialization;
</pre>
<pre class="alteven">
<span class="lnum">   4:</span> <span class="kwrd">using</span> System.Text;
</pre>
<pre class="alt">
<span class="lnum">   5:</span> <span class="kwrd">using</span> System.Xml;
</pre>
<pre class="alteven">
<span class="lnum">   6:</span> <span class="kwrd">using</span> System.Web;
</pre>
<pre class="alt">
<span class="lnum">   7:</span>&nbsp;
</pre>
<pre class="alteven">
<span class="lnum">   8:</span> <span class="kwrd">namespace</span> SquaredRoot.Helper
</pre>
<pre class="alt">
<span class="lnum">   9:</span> {
</pre>
<pre class="alteven">
<span class="lnum">  10:</span>   <span class="kwrd">public</span> <span class="kwrd">class</span> SessionHelper
</pre>
<pre class="alt">
<span class="lnum">  11:</span>   {
</pre>
<pre class="alteven">
<span class="lnum">  12:</span>&nbsp;
</pre>
<pre class="alt">
<span class="lnum">  13:</span>     <span class="kwrd">private</span> SessionHelper(){}
</pre>
<pre class="alteven">
<span class="lnum">  14:</span>&nbsp;
</pre>
<pre class="alt">
<span class="lnum">  15:</span>         <span class="kwrd">public</span> <span class="kwrd">static</span> T Get&lt;T&gt;( <span class="kwrd">string</span> key ) <span class="kwrd">where</span> T : <span class="kwrd">class</span>
</pre>
<pre class="alteven">
<span class="lnum">  16:</span>         {
</pre>
<pre class="alt">
<span class="lnum">  17:</span>&nbsp;
</pre>
<pre class="alteven">
<span class="lnum">  18:</span>             <span class="kwrd">object</span> o = HttpContext.Current.Session[key];
</pre>
<pre class="alt">
<span class="lnum">  19:</span>             <span class="kwrd">if</span>( o == <span class="kwrd">null</span> )
</pre>
<pre class="alteven">
<span class="lnum">  20:</span>                 <span class="kwrd">return</span> <span class="kwrd">default</span>( T );
</pre>
<pre class="alt">
<span class="lnum">  21:</span>&nbsp;
</pre>
<pre class="alteven">
<span class="lnum">  22:</span>             <span class="kwrd">if</span>( o.GetType() == <span class="kwrd">typeof</span>( <span class="kwrd">string</span> ) &amp;&amp; <span class="kwrd">typeof</span>( T ) != <span class="kwrd">typeof</span>( <span class="kwrd">string</span> ) )
</pre>
<pre class="alt">
<span class="lnum">  23:</span>             {
</pre>
<pre class="alteven">
<span class="lnum">  24:</span>                 <span class="kwrd">string</span> s = o <span class="kwrd">as</span> <span class="kwrd">string</span>;
</pre>
<pre class="alt">
<span class="lnum">  25:</span>                 Stream stream = <span class="kwrd">new</span> MemoryStream( Encoding.Unicode.GetBytes(s) );
</pre>
<pre class="alteven">
<span class="lnum">  26:</span>                 XmlDictionaryReader xml = XmlDictionaryReader.CreateTextReader( stream, <span class="kwrd">new</span> XmlDictionaryReaderQuotas() );
</pre>
<pre class="alt">
<span class="lnum">  27:</span>                 DataContractSerializer dcs = <span class="kwrd">new</span> DataContractSerializer( <span class="kwrd">typeof</span>( T ) );
</pre>
<pre class="alteven">
<span class="lnum">  28:</span>                 T t = (T)dcs.ReadObject( xml, <span class="kwrd">true</span> );
</pre>
<pre class="alt">
<span class="lnum">  29:</span>                 xml.Close();
</pre>
<pre class="alteven">
<span class="lnum">  30:</span>                 <span class="kwrd">return</span> t;
</pre>
<pre class="alt">
<span class="lnum">  31:</span>             }
</pre>
<pre class="alteven">
<span class="lnum">  32:</span>             <span class="kwrd">else</span>
</pre>
<pre class="alt">
<span class="lnum">  33:</span>                 <span class="kwrd">return</span> o <span class="kwrd">as</span> T;
</pre>
<pre class="alteven">
<span class="lnum">  34:</span>&nbsp;
</pre>
<pre class="alt">
<span class="lnum">  35:</span>         }
</pre>
<pre class="alteven">
<span class="lnum">  36:</span>&nbsp;
</pre>
<pre class="alt">
<span class="lnum">  37:</span>         <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">bool</span> HasClassAttribute( <span class="kwrd">object</span> o, Type attribute, <span class="kwrd">bool</span> inherit )
</pre>
<pre class="alteven">
<span class="lnum">  38:</span>         {
</pre>
<pre class="alt">
<span class="lnum">  39:</span>             <span class="kwrd">return</span> ( o.GetType().GetCustomAttributes( attribute, inherit ).Length &gt; 0 );
</pre>
<pre class="alteven">
<span class="lnum">  40:</span>         }
</pre>
<pre class="alt">
<span class="lnum">  41:</span>&nbsp;
</pre>
<pre class="alteven">
<span class="lnum">  42:</span>         <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Set( <span class="kwrd">string</span> key, <span class="kwrd">object</span> item )
</pre>
<pre class="alt">
<span class="lnum">  43:</span>         {
</pre>
<pre class="alteven">
<span class="lnum">  44:</span>&nbsp;
</pre>
<pre class="alt">
<span class="lnum">  45:</span>             <span class="kwrd">object</span> <span class="kwrd">value</span> = item;
</pre>
<pre class="alteven">
<span class="lnum">  46:</span>             <span class="kwrd">if</span>( HasClassAttribute( item, <span class="kwrd">typeof</span>(DataContractAttribute), <span class="kwrd">false</span> )
</pre>
<pre class="alt">
<span class="lnum">  47:</span>         &amp;&amp; !HasClassAttribute( item, <span class="kwrd">typeof</span>(SerializableAttribute), <span class="kwrd">false</span> ) )
</pre>
<pre class="alteven">
<span class="lnum">  48:</span>             {
</pre>
<pre class="alt">
<span class="lnum">  49:</span>                 DataContractSerializer dcs = <span class="kwrd">new</span> DataContractSerializer(item.GetType());
</pre>
<pre class="alteven">
<span class="lnum">  50:</span>                 StringBuilder sb = <span class="kwrd">new</span> StringBuilder();
</pre>
<pre class="alt">
<span class="lnum">  51:</span>                 XmlWriter writer = XmlWriter.Create(sb);
</pre>
<pre class="alteven">
<span class="lnum">  52:</span>                 dcs.WriteObject( writer, item );
</pre>
<pre class="alt">
<span class="lnum">  53:</span>                 writer.Close();
</pre>
<pre class="alteven">
<span class="lnum">  54:</span>                 <span class="kwrd">value</span> = sb.ToString();
</pre>
<pre class="alt">
<span class="lnum">  55:</span>             }
</pre>
<pre class="alteven">
<span class="lnum">  56:</span>&nbsp;
</pre>
<pre class="alt">
<span class="lnum">  57:</span>             <span class="kwrd">if</span>( HttpContext.Current.Session[key] == <span class="kwrd">null</span> )
</pre>
<pre class="alteven">
<span class="lnum">  58:</span>         HttpContext.Current.Session.Add( key, <span class="kwrd">value</span> );
</pre>
<pre class="alt">
<span class="lnum">  59:</span>             <span class="kwrd">else</span>
</pre>
<pre class="alteven">
<span class="lnum">  60:</span>         HttpContext.Current.Session[key] = <span class="kwrd">value</span>;
</pre>
<pre class="alt">
<span class="lnum">  61:</span>&nbsp;
</pre>
<pre class="alteven">
<span class="lnum">  62:</span>         }
</pre>
<pre class="alt">
<span class="lnum">  63:</span>&nbsp;
</pre>
<pre class="alteven">
<span class="lnum">  64:</span>     }
</pre>
<pre class="alt">
<span class="lnum">  65:</span> }
</pre>
</div>
</div>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2008/01/30/storing-linq-objects-in-sql-based-session-state/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Add/Update/Delete with LINQPad</title>
		<link>http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/</link>
		<comments>http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 06:42:00 +0000</pubDate>
		<dc:creator>Troy Goode</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">/post/2007/12/05/AddUpdateDelete-with-LINQPad.aspx</guid>
		<description><![CDATA[Danny Douglass recently posted about LINQPad, a query tool that understands LINQ. I spent some time today using it and am very impressed. It did take a while, however, to look through the numerous sample scripts and figure out exactly how to insert/update/delete data, so I thought I would share what I&#39;ve found. The biggest [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.dannydouglass.com">Danny Douglass</a> recently <a href="http://www.dannydouglass.com/post/2007/12/LINQ-Editor-and-Quiz.aspx">posted</a> about <a href="http://www.linqpad.net">LINQPad</a>, a query tool that understands LINQ. I spent some time today using it and am very impressed. It did take a while, however, to look through the numerous sample scripts and figure out exactly how to insert/update/delete data, so I thought I would share what I&#39;ve found. The biggest changes between working in LINQ-To-SQL in a Visual Studio 2008 project and writing a LINQ query in LINQPad are the lack of a <strong>DataContext, </strong>the difference between <strong>C# Expressions</strong> and <strong>C# Statements</strong>, and the addition of a <strong>Dump</strong> command.
</p>
<p>
<strong>No Data Context:</strong> When you need to interact with a database via LINQ in a Visual Studio 2008 project, you do so by creating a LINQ-To-SQL DBML file that generates a data context for you. This data context is in charge of maintaining your database connection and is what you use to submit changes to the database. Because there is no data context readily available to you (and no .dbml file) in LINQPad, the way you go about this is slightly different. There is a globally-scoped subroutine, &quot;SubmitChanges(),&quot; that should be called whenever you wish to commit an action to the database.
</p>
<p>
<strong>C# Expressions vs. C# Statements:</strong> By default LINQPad opens in &quot;C# Expression&quot; mode. In this mode you can type a simple query like &quot;from r in Regions select r&quot; and run it to see the results. As far as I can tell there is no way to insert/update/delete data in this mode. By switching to &quot;C# Statement(s)&quot; mode you are able to declare variables, control flow statements, and reference objects; this is the mode you need to be in to insert/update/delete data. To enter &quot;C# Statement(s)&quot; mode, select it from the &quot;Type&quot; drop-down box at the top of the window as show below:
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/BasicLINQPadExamples_1BA2/LINQPad_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/BasicLINQPadExamples_1BA2/LINQPad_thumb.jpg" border="0" alt="LINQPad" width="458" height="112" /></a>
</p>
<p>
<strong>object.Dump():</strong> When you write a query in &quot;C# Expression&quot; mode the result of that query is automatically rendered to the Results frame (shown below). Because &quot;C# Statement(s)&quot; mode gives you the capability to run many queries in one execute, displaying the results of those queries must be manually invoked. To do so simply call the &quot;.Dump()&quot; method on the resultset of the query you want to display. &quot;.Dump()&quot; is implemented as an extension method available on all objects, so whether you are retrieving a single object, a list of objects, or an anonymous type the Dump method will be available to display your data.
</p>
<p>
<a href="/image.axd?picture=WindowsLiveWriter/BasicLINQPadExamples_1BA2/LINQPad-Results_2.jpg"><img style="border-width: 0px" src="/image.axd?picture=WindowsLiveWriter/BasicLINQPadExamples_1BA2/LINQPad-Results_thumb.jpg" border="0" alt="LINQPad-Results" width="238" height="173" /></a>
</p>
<p>
Below I have included examples of several ways to query data, as well as an example each for inserting, updating, and deleting data.
</p>
<p><!--<br />
{rtf1ansiansicpglang1024noproof65001uc1 deff0{fonttbl{f0fnilfcharset0fprq1 Courier New;}}{colortbl;??red0green128blue0;red215green255blue215;red0green0blue255;red255green255blue255;red0green0blue0;red43green145blue175;red163green21blue21;}??fs20 cf1cb2highlight2 // select (LINQ Syntax)par ??cf3cb0highlight0 varcf0  regions =par ??tab cf3 fromcf0  r cf3 incf0  Regionspar ??tab cf3 wherecf0  r.RegionID &gt; 0par ??tab cf3 selectcf0  r;par ??regions.Dump();par ??par ??cf1cb2highlight2 // insertpar ??cf6cb0highlight0 Regioncf0  newRegion = cf3 newcf0  cf6 Regioncf0 ()par ??{par ??tab RegionID = 99,par ??tab RegionDescription = cf7 "Lorem ipsum..."par ??cf0 };par ??Regions.InsertOnSubmit( newRegion );par ??SubmitChanges();par ??par ??cf1cb2highlight2 // select (LINQ Syntax, no temp variable)par ??cf0cb0highlight0 (cf3 fromcf0  r cf3 incf0  Regionspar ??tab cf3 wherecf0  r.RegionID &gt; 0par ??tab cf3 selectcf0  r).Dump();par ??par ??cf1cb2highlight2 // updatepar ??cf6cb0highlight0 Regioncf0  region =par ??tab (cf3 fromcf0  r cf3 incf0  Regionspar ??tab tab cf3 wherecf0  r.RegionID == 99par ??tab tab cf3 selectcf0  r).Single();par ??region.RegionDescription = cf7 "...dolor sit amet..."cf0 ;par ??SubmitChanges();par ??par ??cf1cb2highlight2 // select (.Where Lambda expression)par ??cf0cb0highlight0 ( Regions.Where( r =&gt; r.RegionID &gt; 0 ) ).Dump();par ??par ??cf1cb2highlight2 // deletepar ??cf6cb0highlight0 Regioncf0  removeRegion =par ??tab Regions.Where( r =&gt; r.RegionID == 99 ).Single();par ??Regions.DeleteOnSubmit( removeRegion );par ??SubmitChanges();par ??par ??cf1cb2highlight2 // select (Regions &amp; Territories, joined andpar ??//         combined by anonymous type)par ??cf0cb0highlight0 (cf3 fromcf0  r cf3 incf0  Regionspar ??tab cf3 joincf0  t cf3 incf0  Territoriespar ??tab tab cf3 oncf0  r.RegionID cf3 equalscf0  t.RegionIDpar ??tab cf3 selectcf0  cf3 newcf0 {par ??tab tab Region = r,par ??tab tab Territory = tpar ??tab }).Dump();}<br />
--></p>
<div style="font-size: 9pt; background: white; margin-bottom: 10px; padding-bottom: 10px; color: black; padding-top: 10px; font-family: courier new">
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 1</span>&nbsp;<span style="background: #d7ffd7; color: green">// select (LINQ Syntax)</span>
</p>
<p style="margin: 0px">
<span style="color:#2b91af">&nbsp;&nbsp;&nbsp; 2</span>&nbsp;<span style="color: blue">var</span> regions =
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 3</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">from</span> r <span style="color: blue">in</span> Regions
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 4</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> r.RegionID &gt; 0
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 5</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">select</span> r;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 6</span> regions.Dump();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 7</span>&nbsp;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 8</span>&nbsp;<span style="background: #d7ffd7; color: green">// insert</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp;&nbsp; 9</span>&nbsp;<span style="color: #2b91af">Region</span> newRegion = <span style="color: blue">new</span> <span style="color: #2b91af">Region</span>()
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 10</span> {
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 11</span>&nbsp;&nbsp;&nbsp;&nbsp; RegionID = 99,
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 12</span>&nbsp;&nbsp;&nbsp;&nbsp; RegionDescription = <span style="color: #a31515">&quot;Lorem ipsum&#8230;&quot;</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 13</span> };
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 14</span> Regions.InsertOnSubmit( newRegion );
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 15</span> SubmitChanges();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 16</span>&nbsp;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 17</span>&nbsp;<span style="background: #d7ffd7; color: green">// select (LINQ Syntax, no temp variable)</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 18</span> (<span style="color: blue">from</span> r <span style="color: blue">in</span> Regions
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 19</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> r.RegionID &gt; 0
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 20</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">select</span> r).Dump();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 21</span>&nbsp;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 22</span>&nbsp;<span style="background: #d7ffd7; color: green">// update</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 23</span>&nbsp;<span style="color: #2b91af">Region</span> region =
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 24</span>&nbsp;&nbsp;&nbsp;&nbsp; (<span style="color: blue">from</span> r <span style="color: blue">in</span> Regions
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> r.RegionID == 99
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">select</span> r).Single();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 27</span> region.RegionDescription = <span style="color: #a31515">&quot;&#8230;dolor sit amet&#8230;&quot;</span>;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 28</span> SubmitChanges();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 29</span>&nbsp;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 30</span>&nbsp;<span style="background: #d7ffd7; color: green">// select (.Where Lambda expression)</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 31</span> ( Regions.Where( r =&gt; r.RegionID &gt; 0 ) ).Dump();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 32</span>&nbsp;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 33</span>&nbsp;<span style="background: #d7ffd7; color: green">// delete</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 34</span>&nbsp;<span style="color: #2b91af">Region</span> removeRegion =
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 35</span>&nbsp;&nbsp;&nbsp;&nbsp; Regions.Where( r =&gt; r.RegionID == 99 ).Single();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 36</span> Regions.DeleteOnSubmit( removeRegion );
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 37</span> SubmitChanges();
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 38</span>&nbsp;
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 39</span>&nbsp;<span style="background: #d7ffd7; color: green">// select (Regions &amp; Territories, joined and</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 40</span>&nbsp;<span style="background: #d7ffd7; color: green">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; combined by anonymous type)</span>
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 41</span> (<span style="color: blue">from</span> r <span style="color: blue">in</span> Regions
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 42</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">join</span> t <span style="color: blue">in</span> Territories
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 43</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">on</span> r.RegionID <span style="color: blue">equals</span> t.RegionID
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 44</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">select</span> <span style="color: blue">new</span>{
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 45</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Region = r,
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 46</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Territory = t
</p>
<p style="margin: 0px">
<span style="color: #2b91af">&nbsp;&nbsp; 47</span>&nbsp;&nbsp;&nbsp;&nbsp; }).Dump();
</p>
</div>
<div style="text-align: center;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/" style="border:0; position: relative; top: -2px;"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/" style="border:0;" alt="Kick It on DotNetKicks.com" /></a><a href="http://dotnetshoutout.com/Submit?url=http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/" style="border: 0;"><img src="http://dotnetshoutout.com/image.axd?url=http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/" style="border:0px" alt="Shout It on DotNetShoutOuts.com" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.squaredroot.com/2007/12/05/addupdatedelete-with-linqpad/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
