<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/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>Derik Whittaker : Linq</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx</link><description>Tags: Linq</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Entity Frame Performance Gotcha</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2013/02/12/entity-frame-performance-gotcha.aspx</link><pubDate>Tue, 12 Feb 2013 19:44:19 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:82206</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=82206</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=82206</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2013/02/12/entity-frame-performance-gotcha.aspx#comments</comments><description>&lt;p&gt;I was doing some profiling of our Entity Framework stuff via &lt;a&gt;EFProfiler&lt;/a&gt; the other day and noticed something really odd.&amp;#160; When I was watching the profiler I noticed that I had a query returning back over 1300 results and that was NOT right, I had expected only 1 result.&amp;#160; I looked the resulting sql and immediately I ‘thought’ I knew the issue.&amp;#160; I thought the issue was that I had not setup my where clause correctly in my EF… Turns out I was both right and wrong at the same time. &lt;/p&gt;  &lt;p&gt;Below is a sample of my EF code &lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;** note that I replaced the dynamic variables w/ static for testing inside of &lt;a href="http://www.linqpad.net/"&gt;LinqPad&lt;/a&gt; **&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_49D06E30.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_7F198095.png" width="790" height="66" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When I run these statements above and look at the resulting sql in &lt;a href="http://www.linqpad.net/"&gt;LinqPad&lt;/a&gt; I would expect to see 2 queries.&amp;#160; Sure enough I did have 2 of them.&amp;#160; What shocked me was that the 2nd query ONLY did a where clause filter on ClientID (see below)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_04F4242F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_0E6CE2A5.png" width="397" height="185" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This had a resulting output of below, which is the expected result&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_01FEBF7C.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_610B4CD4.png" width="520" height="64" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;But if you run the sql generated above (2nd statement) you actually get 1327 results.&amp;#160; This tells me that indeed the ENTIRE result for a client was being pull out of the DB, pulled over the wire and filtered down on the client…This is not good.&lt;/p&gt;  &lt;p&gt;Now I tried a few different things to see if I could get different results such as doing a .FirstOrDefault rather than a .Where… No dice&lt;/p&gt;  &lt;p&gt;Finally one of my coworkers found this &lt;a href="http://stackoverflow.com/questions/5173389/linq-to-sql-entityset-is-not-iqueryable-any-workarounds"&gt;Stack Overflow&lt;/a&gt; link which talks directly to this issue.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Turns out this behavior is by design and I had never noticed it before.&amp;#160; I (and 3 of my coworkers mind you) had just assumed that when EF did a lazy load off of a previously fetched IQueryable it would include any filter statements and pass those to the SERVER.&amp;#160; But because the .Images is NOT a IQuerable but rather an EntitySet&amp;lt;T&amp;gt; it did not work as expected.&lt;/p&gt;  &lt;p&gt;To solve this issue I simply just changed my statement to go directly at my EF context for the ONE row I wanted w/ both keys (client and type).&amp;#160; This solved my issue as well as give me a pretty significant performance enhancement.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_2B2657BA.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_777DEB5B.png" width="926" height="65" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Long story short is that if you are going to lazy gather data in EF make sure you are doing your filtering on the server rather than the client and make sure you understand that an EntitySet does NOT do this only an IQuerable does this.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=82206" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Entity+Framework/default.aspx">Entity Framework</category></item><item><title>Linq rocks, except when it doesn’t.  Can anyone spot the bug in this code?</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/01/21/linq-rocks-except-when-it-doesn-t-can-anyone-spot-the-bug-in-this-code.aspx</link><pubDate>Fri, 21 Jan 2011 23:10:59 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:64752</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=64752</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=64752</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/01/21/linq-rocks-except-when-it-doesn-t-can-anyone-spot-the-bug-in-this-code.aspx#comments</comments><description>&lt;p&gt;I am in the middle of building out some wcf endpoints to support the Dimecasts.net WP7 application I am going to publish.&amp;#160; The code below is some of my main logic which helps grab the episodes.&amp;#160; Because the data for Dimecasts is not very volatile I am going to do some basic (aka primitive) caching.&amp;#160; &lt;/p&gt;  &lt;p&gt;What I am looking to do is save the amount of data which needs to be gathered from the db.&amp;#160; The intent of the code below is to get all data from cache which is greater then the last known episode which is cached on the phone.&amp;#160; If data is found in the cache we will adjust the ‘last known episode’ in order to again reduce the db hit.&amp;#160; However, there is a slight bug.&lt;/p&gt;  &lt;p&gt;Who can spot the bug?&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;        public IList&amp;lt;EpisodeModel&amp;gt; FetchEpisodes( Int32 lastKnownEpisodeId )
        {
            var episodes = new List();
            var cachedEpisodes = Cache.FetchEpisodes()
                .Where( x =&amp;gt; x.EpisodeNumber &amp;gt; lastKnownEpisodeId )
                .OrderBy( x =&amp;gt; x.Id );

            if ( cachedEpisodes.Count() != 0 )
            {
                // reset the last known id in order to reset the reset
                lastKnownEpisodeId = cachedEpisodes.LastOrDefault().Id;

                episodes.AddRange( cachedEpisodes );
            }

            // more code which does not matter here.....

            return episodes;
        }&lt;/pre&gt;

&lt;p&gt;If you know the issue post it in the comments.&amp;#160; If it is not ‘solved’ I will post an update with the solution and the reason.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=64752" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Little nugget of simplification when loading an XDocument</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/07/02/little-nugget-of-simplification-when-loading-an-xdocument.aspx</link><pubDate>Wed, 02 Jul 2008 14:53:27 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:41182</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=41182</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=41182</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/07/02/little-nugget-of-simplification-when-loading-an-xdocument.aspx#comments</comments><description>&lt;p&gt;Today I needed to load in an XML file into memory for later use. At first I was going to use the XmlDocument library (old habits die hard), but later switched over to the XDocument library.&amp;#160; Why, not because one is better (ok, so I think that XDocument is better, but not the point here), but because it caused me less friction (which I guess does make it better).&lt;/p&gt;  &lt;p&gt;Consider this need.&amp;#160; I have a list of XML files on disk and I would like to convert them to a list of XML documents.&amp;#160; &lt;/p&gt;  &lt;p&gt;Below is my original code using the XmlDocument library:&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;...

IList returnDocument = new List&amp;lt; XDocument &amp;gt;();
foreach ( string file in files )
{
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load( file );
	returnDocument.Add( xmlDocument );
}

...&lt;/pre&gt;

&lt;p&gt;Below is my new code using the XDocument library:&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;....
IList returnDocument = new List&amp;lt; XDocument &amp;gt;();

foreach ( string file in files )
{
	returnDocument.Add( XDocument.Load( file ) );
}
....&lt;/pre&gt;

&lt;p&gt;Now there is no real difference in what each example does, but there is a difference in how I have to accomplish my need.&amp;#160; &lt;/p&gt;

&lt;p&gt;Using the XmlDocument library I need to create an instance, load the instance and finally put it into my return array.&lt;/p&gt;

&lt;p&gt;Using the XDocument library, I can simply use the static Load to accomplish this in a single line.&lt;/p&gt;

&lt;p&gt;+1 for less code.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;

&lt;p&gt;&lt;font color="#808080"&gt;&lt;strong&gt;[----- Remember to check out &lt;/strong&gt;&lt;strong&gt;&lt;a href="http://www.dimecasts.net"&gt;DimeCasts.Net&lt;/a&gt;&lt;/strong&gt;&lt;strong&gt; -----]&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=41182" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Using Linq's DataContext to (re)create your database for testing</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/21/using-linq-s-datacontext-to-re-create-your-database-for-testing.aspx</link><pubDate>Wed, 21 May 2008 16:24:12 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40689</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40689</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40689</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/21/using-linq-s-datacontext-to-re-create-your-database-for-testing.aspx#comments</comments><description>&lt;p&gt;If you happen to be using Linq2Sql and are also someone who tests, I have something for you.&amp;#160; You can use Linq&amp;#39;s DataContext to create your database and then populate it with test data prior to each test run.&amp;#160; Note that you can do this same thing when using other OR/M tools such as NHIbernate, so this is nothing new or revolutionary.&lt;/p&gt;  &lt;p&gt;Before I get into the &amp;#39;How To&amp;#39; on doing this, I would like to go over the &amp;#39;Why&amp;#39; I should do this.&lt;/p&gt;  &lt;p&gt;When building out your test suit on any data driven application you are going to need to test data access at some point.&amp;#160; There are really 3 solutions to this problem.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Simply query for known, existing data      &lt;br /&gt;Although this works, this leads to very weak and brittle tests for obvious reasons.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Create/use/delete the data for each of your tests      &lt;br /&gt;This is a better choice, but leads to very heavy tests and can also lead to bad or orphaned data.&amp;#160; Which can be a major pain point if this is done in your development environment.       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;Rebuild the db and test data for each test run.      &lt;br /&gt;To me, this is the best choice as you get a clean slate and any orphaned data will be destroyed after each run.&amp;#160; No harm, no foul &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Ok, on to the How to of this post.&lt;/p&gt;  &lt;p&gt;The first thing you need to do is setup your tests to have your test fixture have FixtureSetup/FixtureTearDown methods to perform building/destroying of the database.&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;private TestsInit _testsInit;

[TestFixtureSetUp]
public void FixtureSetup()
{
    _testsInit = ObjectFactory.GetInstance&amp;lt; TestsInit &amp;gt;();
    _testsInit.InitDatabase();
}

[TestFixtureTearDown]
public void FixtureTeardown()
{
    _testsInit.DestroyDatabase();
}&lt;/pre&gt;

&lt;p&gt;Once you have your Setup/TearDown implemented you need to create the logic to build the database and insert test data.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public void InitDatabase()
{
    var dbContext = new DBContextDataContext( ConfigurationReader.ConnectionString_ForTests );
 
    CreateDatabase( dbContext );
    CreateTypeData( dbContext );
    CreateTestEpisode( dbContext );
}

public void DestroyDatabase()
{
    var dbContext = new DBContextDataContext( ConfigurationReader.ConnectionString_ForTests );

    DeleteDatabase( dbContext );
}

private void CreateDatabase( DataContext dbContext )
{
    DeleteDatabase( dbContext );

    dbContext.CreateDatabase();            
}

public void CreateTypeData( DBContextDataContext dbContext )
{
    dbContext.ExecuteCommand( &amp;quot;INSERT INTO [LevelType]( Name ) VALUES ( &amp;#39;Type 1&amp;#39; )&amp;quot; );
    .....
    dbContext.ExecuteCommand( &amp;quot;INSERT INTO [TagTypes]( Name ) VALUES ( &amp;#39;Type 4&amp;#39; )&amp;quot; );
}

private void CreateTestEpisode( DBContextDataContext dbContext )
{
    Episode episode = new Episode {LevelTypeID = 1, Name = &amp;quot;Test 1&amp;quot;, Description = &amp;quot;Desc 1&amp;quot;, EpisodeNumber = 1, EpisodeDate = DateTime.Now};
    ....

    episode.EpisodeDownloadInformations = new EntitySet&amp;lt; EpisodeDownloadInformation &amp;gt;
                                              {
                                                  new EpisodeDownloadInformation{ FileName = &amp;quot;Foo.wmv&amp;quot;, Size = 9.25m, Time = &amp;quot;10:00&amp;quot; }
                                              };

    dbContext.Episodes.InsertOnSubmit( episode );
    dbContext.SubmitChanges();
}


private void DeleteDatabase( DataContext dbContext )
{
    if ( dbContext.DatabaseExists() )
    {
        dbContext.DeleteDatabase();
    }
}&lt;/pre&gt;

&lt;p&gt;One thing to be careful of here is that you are using an alternate connection string, not the one for your dev/stage/production environment or you will NOT be a happy developer.&amp;#160; Also notice that I both using the ExecuteCmmand statement as well as the object model to insert the test data.&amp;#160; This is done simply to show the concept.&amp;#160; You could also simple script up all the inserts and put them into an external file.&amp;#160; The way you do it is up to you.&lt;/p&gt;

&lt;p&gt;So there you go, you now know how you can use the DataContext to build/destroy your db structure for each test run.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40689" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/TDD/default.aspx">TDD</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>XLinq'n in the new world - Speed comparison with Xml/XPath</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/14/xlinq-n-in-the-new-world-speed-comparison-with-xml-xpath.aspx</link><pubDate>Wed, 14 May 2008 15:02:22 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40565</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40565</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40565</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/14/xlinq-n-in-the-new-world-speed-comparison-with-xml-xpath.aspx#comments</comments><description>&lt;p&gt;Originally this series was meant to be titled &amp;#39;Any thing you can do I can do better&amp;#39;, but to be honest after writing the Xml/XPath examples I realized the XLinq is really no better per say than Xml/XPath.&amp;#160; However, what XLinq does bring to the table that Xml/XPath does not:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Does not need to use the XPath query syntax &lt;/li&gt;    &lt;li&gt;Reads like English (mostly) when creating queries &lt;/li&gt;    &lt;li&gt;Lower barrier to entry for someone new to Xml (my opinion) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For Part 1 of this series check &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/05/12/xlinq-n-in-the-new-world-data-access-comparisons-with-xml-xpath.aspx"&gt;here&lt;/a&gt;     &lt;br /&gt;For Part 2 of this series check &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/05/13/xlinq-n-in-the-new-world-data-manipulation-comparisons-with-xml-xpath.aspx"&gt;here&lt;/a&gt;     &lt;br /&gt;For Part 3 of this series check &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/05/14/xlinq-n-in-the-new-world-structure-modification-comparisons-with-xml-xpath.aspx"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In Part 4 of this little mini-series we will examine the speed differences between XLinq and Xml/XPath.&lt;/p&gt;  &lt;p&gt;Before I get started I will show you a sample of the XML document I am using.&amp;#160; The sample document I am using today is 5.25mb in size.&lt;/p&gt;  &lt;p&gt;&amp;lt;?xml version=&amp;#39;1.0&amp;#39;?&amp;gt;    &lt;br /&gt;&amp;lt;root&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;system&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber Type=&amp;quot;Random&amp;quot;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;63425813&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@comcast.net&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;RAY&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Bob&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;33232&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;418444&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum&amp;gt;0&amp;lt;/ProgNum&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;1271728821&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@yahoo.com&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;JOHN&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Foo&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;1254512&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;1033488&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;/system&amp;gt;     &lt;br /&gt;&amp;lt;/root&amp;gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example 1 - Loading a XML document from Resource Stream&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There is no real code to show here, but I do have some speed numbers to chat about.&amp;#160; I ran my test 2 times. The first time I loaded into a XLinq object first then into an XmlDocument object and for the second run, I swapped the order.&lt;/p&gt;  &lt;p&gt;As it turns out the first document to load each time is slower, but you can get a look at the speed difference by looking at the delta between each load time.&lt;/p&gt;  &lt;p&gt;Load Time&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2"&gt;     &lt;tr&gt;       &lt;td&gt;XmlDocument&lt;/td&gt;        &lt;td&gt;515.00&lt;/td&gt;        &lt;td&gt;327.72&lt;/td&gt;        &lt;td&gt;187.27 (Delta)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;XLinq&lt;/td&gt;        &lt;td&gt;358.94&lt;/td&gt;        &lt;td&gt;546.2135&lt;/td&gt;        &lt;td&gt;187.27 (Delta)&lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;By looking at the above, there is no real speed difference, which I expected. (Time above is in milliseconds)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example 2 - Finding a Unique value in the Xml Document&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;For this example I decided to try to find a single node inside my larger xml file.&amp;#160; In order to try to get good sample I ran each test 5 times and the times below are the average of all those runs&lt;/p&gt;  &lt;p&gt;Run time (5 run avg)&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2"&gt;     &lt;tr&gt;       &lt;td&gt;XLinq&lt;/td&gt;        &lt;td&gt;44.62&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Xml/XPath&lt;/td&gt;        &lt;td&gt;125.65&lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;I have to say that I was kinda surprised by the results.&amp;#160; I figured that the Xml/XPath way would be a little faster, but turns out it is 3 times slower. (Time above is in milliseconds). ** NOTE *** The times above do NOT include the time needed to load the XML document into the correct object model.&lt;/p&gt;  &lt;p&gt;From the information in this mini-series i would conclude that if you need to do any type of XML reading/manipulation/creation I would make the jump over to using XLinq as your preferred way of doing so.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40565" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Mini-Series/default.aspx">Mini-Series</category></item><item><title>XLinq'n in the new world - Structure Modification comparisons with Xml/XPath</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/14/xlinq-n-in-the-new-world-structure-modification-comparisons-with-xml-xpath.aspx</link><pubDate>Wed, 14 May 2008 12:21:19 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40559</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40559</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40559</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/14/xlinq-n-in-the-new-world-structure-modification-comparisons-with-xml-xpath.aspx#comments</comments><description>&lt;p&gt;Originally this series was meant to be titled &amp;#39;Any thing you can do I can do better&amp;#39;, but to be honest after writing the Xml/XPath examples I realized the XLinq is really no better per say than Xml/XPath.&amp;#160; However, what XLinq does bring to the table that Xml/XPath does not:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Does not need to use the XPath query syntax &lt;/li&gt;    &lt;li&gt;Reads like English (mostly) when creating queries &lt;/li&gt;    &lt;li&gt;Lower barrier to entry for someone new to Xml (my opinion) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For Part 1 of this series check &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/05/12/xlinq-n-in-the-new-world-data-access-comparisons-with-xml-xpath.aspx"&gt;here&lt;/a&gt;     &lt;br /&gt;For Part 2 of this series check &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/05/13/xlinq-n-in-the-new-world-data-manipulation-comparisons-with-xml-xpath.aspx"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In Part 3 of this little mini series we will examine some common data creation scenario&amp;#39;s and how XLinq&amp;#39;s syntax compares to that of standard Xml/XPath access.&amp;#160; In general will we review how to modify the structure of an existing Xml document.&lt;/p&gt;  &lt;p&gt;Before I get started I will show you a sample of the XML document I am using.&lt;/p&gt;  &lt;p&gt;&amp;lt;?xml version=&amp;#39;1.0&amp;#39;?&amp;gt;    &lt;br /&gt;&amp;lt;root&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;system&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber Type=&amp;quot;Random&amp;quot;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;63425813&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@comcast.net&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;RAY&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Bob&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;33232&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;418444&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum&amp;gt;0&amp;lt;/ProgNum&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;1271728821&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@yahoo.com&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;JOHN&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Foo&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;1254512&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;1033488&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;/system&amp;gt;     &lt;br /&gt;&amp;lt;/root&amp;gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example 1 - Adding a New Node w/ Element&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;via Xml/XPath&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;// notice the @ is missing on the status -- this is how u search a node
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
XmlNodeList xmlNodeList = xmlDocument.SelectNodes( &amp;quot;//list&amp;quot; );

foreach ( XmlElement element in xmlNodeList )
{
    XmlNode newNode = xmlDocument.CreateNode( XmlNodeType.Element, &amp;quot;NewElement&amp;quot;, string.Empty );
    XmlAttribute nodeAttribute = xmlDocument.CreateAttribute( &amp;quot;MyAttribute&amp;quot; );
    nodeAttribute.Value = &amp;quot;MyValue&amp;quot;;

    newNode.Attributes.Append( nodeAttribute );

    element.AppendChild( newNode );
}&lt;/pre&gt;

&lt;p&gt;Via XLinq&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();

IEnumerable item = from doc in xDocument.Descendants( &amp;quot;list&amp;quot; )
                             select doc;

foreach ( XElement xElement in item )
{
    xElement.Add( new XElement( &amp;quot;NewElement&amp;quot;,
                                new XAttribute( &amp;quot;MyAttribute&amp;quot;, &amp;quot;MyValue&amp;quot; ),
                                new XElement( &amp;quot;ChildElement&amp;quot;, &amp;quot;ChildValue&amp;quot;)) );
}

//ChildValue&lt;/pre&gt;

&lt;p&gt;One of the big differences you will notice when trying to add nodes/elements to an existing Xml doc with XLinq vs. Xml/XPath is the that with XLinq it is more &amp;#39;fluent&amp;#39; then with Xml/XPath.&amp;#160; To add nodes/elements with Xml/XPath you need to create the nodes/elements via the XMLdocument object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2 - Deleting and Node w/ Elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;via Xml/XPath&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;// notice the @ is missing on the status -- this is how u search a node
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
XmlNodeList xmlNodeList = xmlDocument.SelectNodes( &amp;quot;//subscribers/subscriber[Status=&amp;#39;InActive&amp;#39;]&amp;quot; );

foreach ( XmlElement element in xmlNodeList )
{
    element.RemoveAll();
}&lt;/pre&gt;

&lt;p&gt;Via XLinq&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();

IEnumerable&amp;lt; XElement &amp;gt; item = from doc in xDocument.Descendants( &amp;quot;subscriber&amp;quot; )
                               let status = (string) doc.Element( &amp;quot;Status&amp;quot; )
                               where status == &amp;quot;InActive&amp;quot;
                               select doc;

foreach ( XElement xElement in item )
{
    xElement.Remove();
}&lt;/pre&gt;

&lt;p&gt;Besides the way you navigate to the node/element you would like to remove the syntax to delete/remove the nodes are really no different.&amp;#160; But, again with XLinq you do not need to know XPath in order to navigate to the correct location in the document.&lt;/p&gt;

&lt;p&gt;Well, this pretty much wraps up my little mini-series on XLinq&amp;#39;n in the new world.&amp;#160; I hope that in the past 3 posts you have learned a little about how to use XLinq and how it differs from Xml/XPath.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40559" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Mini-Series/default.aspx">Mini-Series</category></item><item><title>XLinq'n in the new world - Data manipulation comparisons with Xml/XPath</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/13/xlinq-n-in-the-new-world-data-manipulation-comparisons-with-xml-xpath.aspx</link><pubDate>Tue, 13 May 2008 10:37:51 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40487</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40487</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40487</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/13/xlinq-n-in-the-new-world-data-manipulation-comparisons-with-xml-xpath.aspx#comments</comments><description>&lt;p&gt;Originally this series was meant to be titled &amp;#39;Any thing you can do I can do better&amp;#39;, but to be honest after writing the Xml/XPath examples I realized the XLinq is really no better per say than Xml/XPath.&amp;#160; However, what XLinq does bring to the table that Xml/XPath does not:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Does not need to use the XPath query syntax &lt;/li&gt;    &lt;li&gt;Reads like English (mostly) when creating queries &lt;/li&gt;    &lt;li&gt;Lower barrier to entry for someone new to Xml (my opinion) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For Part 1 of this series check &lt;a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/05/12/xlinq-n-in-the-new-world-data-access-comparisons-with-xml-xpath.aspx"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In Part 2 of this little mini series we will examine some common data manipulation scenario&amp;#39;s and how XLinq&amp;#39;s syntax compares to that of standard Xml/XPath access.&amp;#160; In general will we review how do update the content of both elements and attributes in an XML document.&lt;/p&gt;  &lt;p&gt;Before I get started I will show you a sample of the XML document I am using.&lt;/p&gt;  &lt;p&gt;&amp;lt;?xml version=&amp;#39;1.0&amp;#39;?&amp;gt;    &lt;br /&gt;&amp;lt;root&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;system&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber Type=&amp;quot;Random&amp;quot;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;63425813&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@comcast.net&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;RAY&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Bob&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;33232&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;418444&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum&amp;gt;0&amp;lt;/ProgNum&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;1271728821&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@yahoo.com&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;JOHN&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Foo&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;1254512&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;1033488&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;/system&amp;gt;     &lt;br /&gt;&amp;lt;/root&amp;gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example 1 - Updating Element Values&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;via Xml/XPath&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;// notice the @ is missing on the status -- this is how u search a node
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
XmlNodeList xmlNodeList = xmlDocument.SelectNodes( &amp;quot;//subscribers/subscriber[Status=&amp;#39;InActive&amp;#39;]&amp;quot; );

// Attribute value before updates
// 
// 

foreach ( XmlElement element in xmlNodeList )
{
    // this will only set the value.  if the node does NOT exist you will get an exception.
    element.SelectSingleNode( &amp;quot;//Status&amp;quot; ).InnerText = &amp;quot;Active&amp;quot;;
}

// Attribute values afterupdate
// &lt;/pre&gt;

&lt;p&gt;via XLinq&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();

var item = from doc in xDocument.Descendants( &amp;quot;subscriber&amp;quot; )
           let status = ( string )doc.Element( &amp;quot;Status&amp;quot; )
           where status == &amp;quot;InActive&amp;quot;
           select doc;

// Attribute value before updates
// InActive

foreach ( XElement xElement in item )
{
    // this will set the value or create it if the element does not exist
    xElement.SetElementValue( &amp;quot;Status&amp;quot;, &amp;quot;Active&amp;quot; );

    // Another option is to go after the element directly, but this requires a 
    //  null check first
    if ( xElement.Element( &amp;quot;Status&amp;quot; ) != null )
    {
        xElement.Element( &amp;quot;Status&amp;quot; ).SetValue( &amp;quot;Active&amp;quot; );
    }

}

// Attribute values after update
// Active&lt;/pre&gt;

&lt;p&gt;As you can see the differences between using XLinq and Xml/XPath for updating elements is not the big.&amp;#160; However, again you have to know/understand how to use XPath to find your nodes.&amp;#160; There is ONE thing you need to be aware of though.&amp;#160; When trying to update an element via XPath the element MUST exist.&amp;#160; If it does not you will get a runtime exception.&amp;#160; With XLinq there is a way to do this and not have to worry about that.&amp;#160; To me that is pretty nice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2 - Updating Attribute Values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;via Xml/XPath&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;// notice the @ is missing on the status -- this is how u search a node
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
XmlNodeList xmlNodeList = xmlDocument.SelectNodes( &amp;quot;//subscribers/subscriber[Status=&amp;#39;InActive&amp;#39;]&amp;quot; );

// Attribute value before updates
// 

foreach ( XmlElement element in xmlNodeList )
{
    // this will set the value or create it if the attribute does not exist
    element.SetAttribute( &amp;quot;Type&amp;quot;, &amp;quot;SomeValue&amp;quot; );
}

// Attribute values afterupdate
// &lt;/pre&gt;

&lt;p&gt;via XLinq&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();

var item = from doc in xDocument.Descendants( &amp;quot;subscriber&amp;quot; )
           let status = ( string )doc.Element( &amp;quot;Status&amp;quot; )
           where status == &amp;quot;InActive&amp;quot;
           select doc;

// Attribute value before updates
// 

foreach ( XElement xElement in item )
{
    // this will set the value or create it if the attribute does not exist
    xElement.SetAttributeValue( &amp;quot;Type&amp;quot;, &amp;quot;MyValue&amp;quot; );

    // Another option is to go after the attribute directly, but this requires a 
    //  null check first
    if ( xElement.Attribute( &amp;quot;Type&amp;quot; ) != null )
    {
        xElement.Attribute( &amp;quot;Type&amp;quot; ).SetValue( &amp;quot;SomeValue&amp;quot; );
    }
    
}

// Attribute values afterupdate
// &lt;/pre&gt;

&lt;p&gt;Above shows you that updating attributes are almost identical and in this case there is no need to know/understand XPath in order to select the single node you are trying to update. &lt;/p&gt;

&lt;p&gt;Well, above I have shown you a few simple examples that compare Xml/Path to XLinq, I hope you find this useful.&amp;#160; The next post will compare how to modify the structure of a document using the both Xml/XPath and XLinq.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40487" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Mini-Series/default.aspx">Mini-Series</category></item><item><title>XLinq'n in the new world - Data access comparisons with Xml/XPath</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/12/xlinq-n-in-the-new-world-data-access-comparisons-with-xml-xpath.aspx</link><pubDate>Mon, 12 May 2008 10:15:22 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40471</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40471</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40471</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/12/xlinq-n-in-the-new-world-data-access-comparisons-with-xml-xpath.aspx#comments</comments><description>&lt;p&gt;Originally this series was meant to be titled &amp;#39;Any thing you can do I can do better&amp;#39;, but to be honest after writing the Xml/XPath examples I realized the XLinq is really no better per say than Xml/XPath.&amp;#160; However, what XLinq does bring to the table that Xml/XPath does not:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Does not need to use the XPath query syntax &lt;/li&gt;    &lt;li&gt;Reads like English (mostly) when creating queries &lt;/li&gt;    &lt;li&gt;Lower barrier to entry for someone new to Xml (my opinion) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In Part 1 of this little mini series we will examine some common data access scenario&amp;#39;s and how XLinq&amp;#39;s syntax compares to that of standard Xml/XPath access.&lt;/p&gt;  &lt;p&gt;Before I get started I will show you a sample of the XML document I am using.&lt;/p&gt;  &lt;p&gt;&amp;lt;?xml version=&amp;#39;1.0&amp;#39;?&amp;gt;    &lt;br /&gt;&amp;lt;root&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;system&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber Type=&amp;quot;Random&amp;quot;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;63425813&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@comcast.net&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;RAY&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Bob&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;33232&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;418444&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum&amp;gt;0&amp;lt;/ProgNum&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;list&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;id&amp;gt;1271728821&amp;lt;/id&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Email__Address&amp;gt;FakeAddress@yahoo.com&amp;lt;/Email__Address&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EmailType&amp;gt;HTML&amp;lt;/EmailType&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Status&amp;gt;InActive&amp;lt;/Status&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;First__Name&amp;gt;JOHN&amp;lt;/First__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Last__Name&amp;gt;Foo&amp;lt;/Last__Name&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIN__Code&amp;gt;NaN&amp;lt;/PIN__Code&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PIPIN&amp;gt;1254512&amp;lt;/PIPIN&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;UID&amp;gt;1033488&amp;lt;/UID&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ProgNum /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Title /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Username /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Password /&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscriber&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/subscribers&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/list&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;/system&amp;gt;     &lt;br /&gt;&amp;lt;/root&amp;gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Example 1 - Simple data access&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;via Xml/XPath&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();

// notice the @ is on the status -- this is how u search a element
Int32 listCount = xmlDocument.SelectNodes( &amp;quot;//list&amp;quot; ).Count;
Int32 subscribersCount = xmlDocument.SelectNodes( &amp;quot;//subscriber[@Type=&amp;#39;Random&amp;#39;]&amp;quot; ).Count;&lt;/pre&gt;

&lt;p&gt;via XLinq&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();

Int32 listCount = xDocument.Descendants( &amp;quot;list&amp;quot; ).Count();
Int32 subscribersCount = xDocument.Descendants( &amp;quot;subscriber&amp;quot; ).Attributes( &amp;quot;Type&amp;quot; ).Where( e =&amp;gt; e.Value == &amp;quot;Random&amp;quot; ).Count();&lt;/pre&gt;

&lt;p&gt;As you can see both examples are pretty light on the code and are pretty straight forward.&amp;#160; This is one case where the new XLinq syntax is really no better/simpler then the Xml/XPath syntax.&amp;#160; The issue with the XPath syntax is that&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2 - Get a list of nodes that match our search criteria&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;via Xml/Xpath&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;// notice the @ is missing on the status -- this is how u search a node
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
XmlNodeList xmlNodeList = xmlDocument.SelectNodes( &amp;quot;//subscribers/subscriber[Status=&amp;#39;InActive&amp;#39;]&amp;quot; );

foreach ( XmlElement element in xmlNodeList )
{
    var id = element.SelectSingleNode( &amp;quot;//id&amp;quot; ).InnerText;
    var type = element.Attributes[ &amp;quot;Type&amp;quot; ].Value;
}&lt;/pre&gt;

&lt;p&gt;via XLinq&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();

var item = from doc in xDocument.Descendants( &amp;quot;subscriber&amp;quot; )
           let status = (string)doc.Element( &amp;quot;Status&amp;quot; )
           where status == &amp;quot;InActive&amp;quot;
           select doc;

foreach ( XElement xElement in item )
{
    var id = xElement.Element( &amp;quot;id&amp;quot; ).Value;
    var type = xElement.Attribute( &amp;quot;Type&amp;quot; ).Value;
}&lt;/pre&gt;

&lt;p&gt;Now here is an example where I think the XLinq syntax is cleaner and easier to understand.&amp;#160; There is no need to know or understand the XPath syntax, which can be a pain to learn.&amp;#160; Also here you see how XLinq looks a lot like standard SQL syntax and should be familiar to most developers.&lt;/p&gt;

&lt;p&gt;Well, above I have shown you a few simple examples that compare Xml/Path to XLinq, I hope you find this useful.&amp;#160; The next post will compare how to update/add data to a document using the both Xml/XPath and XLinq.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40471" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Mini-Series/default.aspx">Mini-Series</category></item><item><title>Using XLinq to manipulate an XML Document, XLinq U Rock</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/01/using-xlinq-to-manipulate-an-xml-document-xlinq-u-rock.aspx</link><pubDate>Thu, 01 May 2008 21:26:40 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40295</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40295</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40295</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/05/01/using-xlinq-to-manipulate-an-xml-document-xlinq-u-rock.aspx#comments</comments><description>&lt;p&gt;For the first time in a long time I needed to update values inside an XML document.&amp;#160; I have done this type of action many times in the past and have always hated it.&amp;#160; The syntax is clunky and the process is simply painful.&lt;/p&gt;  &lt;p&gt;So today when I realized I need to do this, let me just tell you I was NOT looking forward to writing this code.&amp;#160; Then I remembered that with .Net 3.5 they added &lt;a href="http://xlinq.net/"&gt;XLinq&lt;/a&gt; and it allows you to simply navigate and manipulate XML documents.&amp;#160; This was exactly what I was looking for.&lt;/p&gt;  &lt;p&gt;In order to figure out how to use XLinq to manipulate a file I turned to the book &lt;a href="http://linqinaction.net/"&gt;Linq in Action&lt;/a&gt; to find the answer.&amp;#160; In the very short time I have had this book, I have found it to be very useful.&lt;/p&gt;  &lt;p&gt;Ok, ok, enough chatter, lets get to the problem and how I solved it.&lt;/p&gt;  &lt;p&gt;For my task, I need to open a XML Document that is stored as an embedded resource in our application, make changes to it and pass it along as an XML Document.&amp;#160; The end goal is to take this document and sent it along to one of our 3rd party vendors via the XML Api.&lt;/p&gt;  &lt;p&gt;Here is the Raw XML that needed to be modified&lt;/p&gt;  &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; ?&amp;gt;   &lt;br /&gt;&amp;lt;root&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;authorization&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;username&amp;gt;&amp;lt;/username&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;password&amp;gt;&amp;lt;/password&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;/authorization&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;call&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;call_name&amp;gt;&amp;lt;/call_name&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;call_action&amp;gt;&amp;lt;/call_action&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;search_type&amp;gt;&amp;lt;/search_type&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;search_value&amp;gt;&amp;lt;/search_value&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;search_value2&amp;gt;&amp;lt;/search_value2&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;/root&amp;gt;    &lt;br /&gt;&amp;lt;/root&amp;gt;&lt;/p&gt;  &lt;p&gt;Below is the code needed to update this document with the provided values.&amp;#160; &lt;br /&gt;In order to use XLinq you need to import &lt;font color="#b14314"&gt;System.Xml.Linq&lt;/font&gt;&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;{
    XmlDocument xmlDocument = METHOD TO LOAD XML DOCUMENT HERE

    // Convert to an XDoc so we can process this
    XDocument xDocument = XDocument.Parse( xmlDocument.OuterXml );

    XElement authorizationElement = xDocument.Element( root&amp;quot; ).Element( &amp;quot;authorization&amp;quot; );
    authorizationElement.SetElementValue( &amp;quot;username&amp;quot;, &amp;quot;SomeValue&amp;quot; );
    authorizationElement.SetElementValue( &amp;quot;password&amp;quot;, &amp;quot;SomeValue&amp;quot; );

    XElement systemElement = xDocument.Element( &amp;quot;root&amp;quot; ).Element( &amp;quot;call&amp;quot; );

    systemElement.SetElementValue( &amp;quot;call_name&amp;quot;, &amp;quot;SomeValue&amp;quot; );
    systemElement.SetElementValue( &amp;quot;call_action&amp;quot;, &amp;quot;SomeValue&amp;quot; );
    systemElement.SetElementValue( &amp;quot;search_type&amp;quot;, &amp;quot;SomeValue&amp;quot; );
    systemElement.SetElementValue( &amp;quot;search_value&amp;quot;, &amp;quot;SomeValue&amp;quot; );

    // convert the xDocument back to an XML document for later usage
    xmlDocument.LoadXml( xDocument.ToString() );
    return xmlDocument;
}&lt;/pre&gt;

&lt;p&gt;So as you can see I can very easily and very quickly manipulate this document with the correct values.&amp;#160; And because the syntax is in a fluent style it is VERY easy to read.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40295" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Multi-Column Grouping with Linq2Sql</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/30/multi-column-grouping-with-linq2sql.aspx</link><pubDate>Wed, 30 Apr 2008 12:32:05 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40279</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40279</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40279</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/30/multi-column-grouping-with-linq2sql.aspx#comments</comments><description>&lt;p&gt;Today as I was diving further into my Linq2Sql Odyssey I ran into a need to do grouping on data.&amp;#160; Bud not just single column grouping, I needed to group by multiple columns from multiple tables.&amp;#160; Because it took me a few minutes to get right, I thought I would share my experiences.&lt;/p&gt;  &lt;p&gt;Imagine if you will you have the following SQL statement&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;SELECT  COUNT(*) AS Count,
        cs.SendID,
        cs.Name AS SendName,
        cts.ListID,
        cts.ListName,
        ctom.EmailAddress
FROM    CampaignTrackingOpenedMail AS ctom
        INNER JOIN CampaignTrackingSummary AS cts 
			ON ctom.CampaignTrackingSummaryID = cts.[ID]
        INNER JOIN CampaignSend AS cs 
			ON cts.SendID = cs.SendID
GROUP BY cs.SendID,
        cs.Name,
        cts.ListID,
        cts.ListName,
        ctom.EmailAddress&lt;/pre&gt;

&lt;p&gt;And you would like to turn that into a Linq Statement.&amp;#160; At first I was a little stumped because I was not using the correct syntax.&amp;#160; My first attempt had me trying the following&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;from ctom in CampaignTrackingOpenedMails
join cts in CampaignTrackingSummaries on ctom.CampaignTrackingSummaryID equals cts.ID
join cs in CampaignSends on cts.SendID equals cs.SendID
group ctom by cs.SendID, cs.Name, cts.ListID, cts.ListName, ctom.EmailAddress into emailItems
select new { SendID = emailItems.Key.SendID,
			SendName = emailItems.Key.Name,
			ListID = emailItems.Key.ListID,
			ListName = emailItems.Key.ListName,
			EmailAddress = emailItems.Key.EmailAddress,
			Count = emailItems.Count()
			}&lt;/pre&gt;

&lt;p&gt;After a bit of searching I realized the error of my ways.&amp;#160; I needed to use an anonymous type as my group by object value.&amp;#160; So I made the changes and here is my correct attempt&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;from ctom in CampaignTrackingOpenedMails
join cts in CampaignTrackingSummaries on ctom.CampaignTrackingSummaryID equals cts.ID
join cs in CampaignSends on cts.SendID equals cs.SendID
group ctom by new { cs.SendID, cs.Name, cts.ListID, cts.ListName, ctom.EmailAddress } into emailItems
select new { SendID = emailItems.Key.SendID,
			SendName = emailItems.Key.Name,
			ListID = emailItems.Key.ListID,
			ListName = emailItems.Key.ListName,
			EmailAddress = emailItems.Key.EmailAddress,
			Count = emailItems.Count()
			}&lt;/pre&gt;

&lt;p&gt;A few things to notice here are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The use of anonymous types 
    &lt;br /&gt;group ctom by new&lt;font color="#b14314"&gt; { cs.SendID, cs.Name, cts.ListID, cts.ListName, ctom.EmailAddress }&lt;/font&gt; into emailItems 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;How the group by columns are used 
    &lt;br /&gt;select new { &lt;font color="#b14314"&gt;SendID = emailItems.Key.SendID&lt;/font&gt; ...... &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, hope this helps someone else.&lt;/p&gt;

&lt;p&gt;BTW, If you are NOT using LinqPad for your Linq2Sql stuff, you really need to check it out.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40279" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net+IDE/default.aspx">.Net IDE</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Linq2Sql - Explicit Construction of Entity Exception</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/25/linq2sql-explicit-construction-of-entity-exception.aspx</link><pubDate>Fri, 25 Apr 2008 15:38:27 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40217</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40217</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40217</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/25/linq2sql-explicit-construction-of-entity-exception.aspx#comments</comments><description>&lt;p&gt;Gotta say, I am kinda flustered right now.&amp;#160; Today I was trying to perform what I thought to be a pretty straight forward Linq2Sql query, but turns out it was not.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I have a query that joins a few tables together to get some data, but in place of returning explicit instance of a data entity, I wanted to do a select new XXXX.&amp;#160; I wanted to do this because I had extended the main entity to have a extra property that is needed based on my domain.&amp;#160; And because I did NOT want to create a who new entity, I thought this was the path with the least resistance.&amp;#160; WRONG.&lt;/p&gt;  &lt;p&gt;Here is what I WANTED to do.&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;var items = ( from ctl in DBContextInstance().CampaignTrackingLinks
         join cts in DBContextInstance().CampaignTrackingSummaries on ctl.CampaignTrackingSummaryID equals cts.ID
         join ctcts in DBContextInstance().CampaignTrackingClickThroughStats on cts.ID equals ctcts.CampaignTrackingSummaryID
         where cts.SendID == sendID
         where ctl.Url == ctcts.URL
         select new CampaignTrackingLink
         {
             TotalClicks = ctl.TotalClicks,
             UniqueClicks = ctl.UniqueClicks,
             Url = ctl.Url,
             Alias = ctl.Alias
             PercentOfClicks = ctcts.PercentOfClicks
         }
       ).ToList();&lt;/pre&gt;

&lt;p&gt;However, every time I ran this code I received&amp;#160; the following exception &amp;#39;&lt;em&gt;Explicit construction of entity type &amp;#39;XXXXX.CampaignTrackingLink&amp;#39; in query is not allowed.&amp;#39;.&amp;#160; &lt;/em&gt;What was throwing me at first was I KNEW that my syntax was correct, but it still did not work.&lt;/p&gt;

&lt;p&gt;After a bit of googling I found (&lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2469929&amp;amp;SiteID=1"&gt;here&lt;/a&gt;) something that I think may explain why this is happening.&amp;#160; &lt;/p&gt;

&lt;p&gt;The solution to get this to work was not to create a new instance of my extended Linq entity, but to create a whole new entity that inherits off of my Linq entity.&amp;#160; For some reason this worked&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;    public class CampaignTrackingLinkBreakdown : CampaignTrackingLink
    {
        public double PercentOfClicks { get; set; }
    }&lt;/pre&gt;

&lt;p&gt;It really sucks that I needed to create a new class that inherited off my Linq entity, but you do what you have to get it working.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;var items = ( from ctl in DBContextInstance().CampaignTrackingLinks
         join cts in DBContextInstance().CampaignTrackingSummaries on ctl.CampaignTrackingSummaryID equals cts.ID
         join ctcts in DBContextInstance().CampaignTrackingClickThroughStats on cts.ID equals ctcts.CampaignTrackingSummaryID
         where cts.SendID == sendID
         where ctl.Url == ctcts.URL
         select new CampaignTrackingLinkBreakdown
         {
             TotalClicks = ctl.TotalClicks,
             UniqueClicks = ctl.UniqueClicks,
             Url = ctl.Url,
             Alias = ctl.Alias,
             PercentOfClicks = ctcts.PercentOfClicks
         }
       ).ToList();&lt;/pre&gt;

&lt;p&gt;So as you can see, I get the same end result, but have to jump through a few extra hoops.&lt;/p&gt;

&lt;p&gt;Based on the feedback from the forum link above, this issue is due to the fact that Linq attempts to keep track of all entities for change state reasons and allowing for explicit construction via a query could cause a caching issue.&amp;#160; In my situation, I DO NOT care about change state as this is a read only entity so there will be no updates/inserts done later.&lt;/p&gt;

&lt;p&gt;Oh well, at least now I know the issue and how to work around it.&lt;/p&gt;

&lt;p&gt;If anyone knows of a better/cleaner way to solve this issue, please let me know.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40217" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Anonymous Casting with Linq to Sql</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/09/anonymous-casting-with-linq-to-sql.aspx</link><pubDate>Wed, 09 Apr 2008 22:39:35 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:40010</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=40010</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=40010</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/09/anonymous-casting-with-linq-to-sql.aspx#comments</comments><description>&lt;p&gt;***** Disclaimer ******    &lt;br /&gt;I may be the last person to figure this out, but oh well.     &lt;br /&gt;***** End Disclaimer ******&lt;/p&gt;  &lt;p&gt;Today as I was further exploring the coolness that is Linq to Sql I ran into a little situation.&amp;#160; I was creating a query where I wanted to use the columns form multiple tables to build an object.&amp;#160; Since the results did not map 1 to 1 from a table to a object I figured I would have to do it the mapping the hard way.&amp;#160; Here was the code I originally had.&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;var results = from c in DBContextInstance().Companies
              join pc in DBContextInstance().ProgramControls on c.CoID equals pc.CoID
              select new {c.CoID, c.CoDesc, pc.ProjectNotes, pc.ProjectManager};

List newList = new List&amp;lt; HybridObject &amp;gt;();

foreach ( var result in results )
{
    HybridObject entity = new HybridObject
                              {
                                  CompanyID = result.CoID,
                                  CompanyDesc = result.CoDesc,
                                  ProjectNotes = result.ProjectNotes,
                                  ProjectManager = result.ProjectManager
                              };
    newList.Add( entity  );
}&lt;/pre&gt;

&lt;p&gt;After looking at the code above for a few minutes, I really, really did not like it.&amp;#160; I knew there had to be a better way.&amp;#160; So I started playing around with various ways to do this.&amp;#160; I had a hunch that since I was creating a return value as an Anonymous type, I should be able to create a concrete type in its place.&amp;#160; Sure enough, I could.&amp;#160; With a little help from constructor initializers (sure you could also do this by creating a constructor that took in the values as well).&amp;#160; Take a look at the new, cleaner code below.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;var results = ( from c in DBContextInstance().Companies
              join pc in DBContextInstance().ProgramControls on c.CoID equals pc.CoID
              select new HybridObject
                         {
                                CompanyID = CoID,
                                CompanyDesc = CoDesc,
                                ProjectNotes = ProjectNotes,
                                ProjectManager = ProjectManager
                         } ).ToList();&lt;/pre&gt;

&lt;p&gt;Now if you ask me, the new code is much more concise and much simpler.&amp;#160; It also does not waste CPU cycles, which is always a plus.&lt;/p&gt;

&lt;p&gt;If anyone has a better way, please let me know.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=40010" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Linq and Delayed execution</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx</link><pubDate>Mon, 07 Apr 2008 18:11:57 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39988</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=39988</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=39988</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx#comments</comments><description>&lt;p&gt;If you have not taken a look at Linq (either Linq to objects or Linq to sql) you need to do so.&amp;#160; I would say it is the coolest new feature that was released with .Net 3.5.&amp;#160; But there is one thing that you MUST keep in mind when using Linq.&amp;#160; It has been designed to perform lookups using delayed execution.&amp;#160; When you declare your Linq statement all you are doing is creating the memory representation of the query as an express tree.&amp;#160; The execution of this plan will NOT be executed until the first time you attempt to access the result set of the Linq query.&lt;/p&gt;  &lt;p&gt;But before we get started I want to show the demo object model I will be using for this post.&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;public class Demo
{

    public string Name { get; set;  }
    public string Text { get; set; }
    public Int32 Count { get; set;  }

    public static List BuildDemoList()
    {
        return new List&amp;lt; Demo &amp;gt;
                   {
                       new Demo {Name = &amp;quot;First&amp;quot;, Text = &amp;quot;Some&amp;quot;, Count = 123},
                       new Demo {Name = &amp;quot;Second&amp;quot;, Text = &amp;quot;Other&amp;quot;, Count = 223},
                       new Demo {Name = &amp;quot;Third&amp;quot;, Text = &amp;quot;Value&amp;quot;, Count = 443},
                       new Demo {Name = &amp;quot;Fourth&amp;quot;, Text = &amp;quot;Goes&amp;quot;, Count = 224},
                       new Demo {Name = &amp;quot;Last&amp;quot;, Text = &amp;quot;Here&amp;quot;, Count = 5534}
                   };
    }
}&lt;/pre&gt;

&lt;p&gt;Lets take a look at a few examples to help demonstrate how Linq&amp;#39;s delayed execution works&lt;/p&gt;

&lt;p&gt;This first block simply shows how you can query a collection and spit out the contents&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;List&amp;lt; Demo &amp;gt; values = Demo.BuildDemoList();
var result = from d in values
             where d.Count &amp;gt;= 224
             select d;

foreach ( var demo in result )
{
    System.Diagnostics.Debug.WriteLine( string.Format( &amp;quot;{0}, {1}, {2}&amp;quot;, demo.Name, demo.Text, demo.Count ) );
}

// output
// Third, Value, 443
// Fourth, Goes, 224
// Last, Here, 5534&lt;/pre&gt;

&lt;p&gt;This second block does the same as the ones above, but with a twist.&amp;#160; Because we are changing the value of an item in the list PRIOR to using it we are going to change the results.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;List values = Demo.BuildDemoList();
var result = from d in values
             where d.Count &amp;gt;= 224
             select d;

values[ 2 ].Count = 100;

foreach (var demo in result)
{
    System.Diagnostics.Debug.WriteLine(string.Format(&amp;quot;{0}, {1}, {2}&amp;quot;, demo.Name, demo.Text, demo.Count));
}

// Output
// Fourth, Goes, 224
// Last, Here, 5534&lt;/pre&gt;

&lt;p&gt;This final block appears to be the exact same as the second above, but with one small change.&amp;#160; We have called the .ToList() on the Linq statement.&amp;#160; By calling the .ToList() we are forcing the execution of the query immediately. Now when we change a value in the underlying collection and then iterate over the results we will NOT get the changes.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;List values = Demo.BuildDemoList();
var result = ( from d in values
             where d.Count &amp;gt;= 224
             select d ).ToList();

values[2].Count = 100;

foreach (var demo in result)
{
    System.Diagnostics.Debug.WriteLine(string.Format(&amp;quot;{0}, {1}, {2}&amp;quot;, demo.Name, demo.Text, demo.Count));
}

// Output
// Third, Value, 100
// Fourth, Goes, 224
// Last, Here, 5534&lt;/pre&gt;

&lt;p&gt;The thing I would like to stress here is that Linq by default will NOT execute the query prior to the first use.&amp;#160; I know this will cause issues for some, and may be very hard to spot if you do not understand this fact.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39988" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Featured/default.aspx">Featured</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Using Linq to access the file system</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/30/using-linq-to-access-the-file-system.aspx</link><pubDate>Sun, 30 Mar 2008 21:31:14 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39872</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=39872</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=39872</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/30/using-linq-to-access-the-file-system.aspx#comments</comments><description>Again, this post is another in the recent string of How-to&amp;#39;s.&amp;nbsp; But it is just so hard to resist.&amp;nbsp; .Net 3.5 has so many cool features, it is just fun to explore them all.  &lt;p&gt;In today&amp;#39;s post I thought it would be fun to use Linq to access the file system.&amp;nbsp; I thought I would show how to query both for folders and for files.&amp;nbsp; In each example I will give the non-Linq way and the Linq way.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Searching for a giving Directory (by name)&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Non-Linq way&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;FileInfo[] files = new DirectoryInfo( @&amp;quot;K:\SomeFolder&amp;quot; ).GetFiles();

foreach ( FileInfo file in files )
{
        if ( file.Name == &amp;quot;SomeFileName.txt&amp;quot; )
	{
        	have a match, do something now
	}
}
&lt;/pre&gt;
&lt;p&gt;Linq way&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;var files = from file in new DirectoryInfo(@&amp;quot;K:\SomeFolder&amp;quot;).GetFiles()
	where file.Name == &amp;quot;SomeFileName.txt&amp;quot;
        select file;
&lt;/pre&gt;
&lt;p&gt;Searching for a given File (by name)&lt;/p&gt;
&lt;p&gt;Non-Linq way&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;DirectoryInfo[] di = new DirectoryInfo( @&amp;quot;K:\SomeFolder&amp;quot; ).GetDirectories();

foreach ( DirectoryInfo directoryInfo in di )
{
        if ( directoryInfo.Name == &amp;quot;SubFolderName&amp;quot;)
	{
		// have a match, do something now
	}
}
&lt;/pre&gt;
&lt;p&gt;Linq way &lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;var folders = from folder in new DirectoryInfo(@&amp;quot;K:\SomeFolder&amp;quot;).GetDirectories()
	where folder.Name == &amp;quot;SubFolderName&amp;quot;
        select folder;
&lt;/pre&gt;
&lt;p&gt;The above Linq code is not better or worse really, just different.&amp;nbsp; And like I said before, if you are going to be using .Net 3.5, might as well use the new features.&lt;/p&gt;
&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39872" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Sorting List's the .Net 3.5 Way</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/sorting-list-s-the-net-3-5-way.aspx</link><pubDate>Sat, 29 Mar 2008 02:09:13 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39854</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=39854</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=39854</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/sorting-list-s-the-net-3-5-way.aspx#comments</comments><description>&lt;p&gt;This post is another in the recent string of How-to&amp;#39;s.&amp;nbsp; But it is just so hard to resist.&amp;nbsp; .Net 3.5 has so many cool features, it is just fun to explore them all.&lt;/p&gt; &lt;p&gt;Ok, so here is the issue we are going to solve today.&amp;nbsp; We have a list of entities and we need to sort them.&amp;nbsp; In this post I will show you 3 different ways to accomplish this task, all producing the same result, but using different techniques (hey &lt;a href="http://msmvps.com/blogs/peterritchie/default.aspx" target="_blank"&gt;Peter&lt;/a&gt;, go ahead and show me a 4th and better way :) ).&lt;/p&gt; &lt;p&gt;A little up front declarations.&amp;nbsp; Each method will use the same list, so to save space I will declare this now.&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;List userList = new List
    {
        new User {FirstName = &amp;quot;Andy&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Opie&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Bee&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Barney&amp;quot;, LastName = &amp;quot;Fife&amp;quot;},
        new User {FirstName = &amp;quot;Goomer&amp;quot;, LastName = &amp;quot;Pile&amp;quot;}
};
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 1 -- Using the Sort with IComarer&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public void Sort_OldSchool()
{
    userList.Sort(new UserCompare());
}

// Sort class
private class UserCompare : IComparer&amp;lt;User&amp;gt;
 {
    public int Compare( User x, User y )
    {
    return x.LastName.CompareTo( y.LastName );
}
&lt;/pre&gt;
&lt;p&gt;The above works, but requires you to create a custom class that implements the IComparer&amp;lt;User&amp;gt; interface to do the trick.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 2 -- Using Lambda and extension methods&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[Test]
public void Sort_UsingLambda()
{
	userList = userList.OrderBy( user =&amp;gt; user.LastName ).ToList();
}
&lt;/pre&gt;
&lt;p&gt;This way works great, it is clean and very readable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 3 -- Use Linq2Objects&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[Test]
public void Sort_UsingLinq()
{
	userList = ( from u in userList
        	orderby u.LastName, u.FirstName
                select u ).ToList();
}
&lt;/pre&gt;
&lt;p&gt;By using Linq you open yourself up to a whole new level of flexibility. You can not only sort (OrderBy), you can filter, return only a select number of rows, etc, etc.&amp;nbsp; Basically you can do anything that Linq provides.&lt;/p&gt;
&lt;p&gt;As you can see from the above, they all accomplish the same goal but method 2 &amp;amp; 3 are just more fun.&amp;nbsp; Besides, if you are going to be using .Net 3.5 you might as well use the new features.&lt;/p&gt;
&lt;p&gt;BTW, all the methods (except 3 since it sorts by LastName and FirstName) above produce the following output.&lt;br /&gt;&lt;em&gt;Barney Fife&lt;br /&gt;Goomer Pile&lt;br /&gt;Andy Tayler&lt;br /&gt;Opie Tayler&lt;br /&gt;Bee Tayler&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39854" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Opinion/default.aspx">Opinion</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Lambda/default.aspx">Lambda</category></item></channel></rss>