<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://azurecoding.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>I Can't Believe That Worked : C#</title><link>http://azurecoding.net/blogs/icbtw/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Silverlight Bing Maps Control and Drag &amp; Drop</title><link>http://azurecoding.net/blogs/icbtw/archive/2010/04/17/silverlight-bing-maps-control-and-drag-amp-drop.aspx</link><pubDate>Sat, 17 Apr 2010 22:57:00 GMT</pubDate><guid isPermaLink="false">6e97e1e3-dc93-4c0d-b73f-e698f7d56013:6234</guid><dc:creator>David Justice</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/rsscomments.aspx?PostID=6234</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/commentapi.aspx?PostID=6234</wfw:comment><comments>http://azurecoding.net/blogs/icbtw/archive/2010/04/17/silverlight-bing-maps-control-and-drag-amp-drop.aspx#comments</comments><description>&lt;p&gt;I was recently asked to build up a Silverlight tool to show a file full 
of addresses on a map. I decided to use the Silverlight bing maps 
control and I must say I was not disappointed with it&amp;#39;s functionality. 
It ended up being extremely simple. The tools used to create this were 
VS2010, Silverlight 4, Bing maps Silverlight control, and the Bing maps 
web services.&lt;/p&gt;...(&lt;a href="http://azurecoding.net/blogs/icbtw/archive/2010/04/17/silverlight-bing-maps-control-and-drag-amp-drop.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://azurecoding.net/aggbug.aspx?PostID=6234" width="1" height="1"&gt;</description><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/Bing+Maps/default.aspx">Bing Maps</category></item><item><title>Genetic Algorithm -- Add Ur Own Func :)</title><link>http://azurecoding.net/blogs/icbtw/archive/2009/06/29/genetic-algorithm-add-ur-own-func.aspx</link><pubDate>Mon, 29 Jun 2009 08:34:00 GMT</pubDate><guid isPermaLink="false">6e97e1e3-dc93-4c0d-b73f-e698f7d56013:1336</guid><dc:creator>David Justice</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/rsscomments.aspx?PostID=1336</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/commentapi.aspx?PostID=1336</wfw:comment><comments>http://azurecoding.net/blogs/icbtw/archive/2009/06/29/genetic-algorithm-add-ur-own-func.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Hi again,&lt;/p&gt;
&lt;p&gt;I was a little bored this weekend and needed to add in some GA functionality to a side project. I felt like it would be best to create a simple executer to deal with the iterative behavior of a genetic algorithm. The&amp;nbsp;&lt;span&gt;executer&amp;nbsp;&lt;/span&gt;take in funcs provided by the consumer and executes based on a simple iterative model. Below is what I came up with. Cheers!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;...(&lt;a href="http://azurecoding.net/blogs/icbtw/archive/2009/06/29/genetic-algorithm-add-ur-own-func.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://azurecoding.net/aggbug.aspx?PostID=1336" width="1" height="1"&gt;</description><enclosure url="http://azurecoding.net/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.00.13.36/GA.zip" length="45494" type="application/x-zip-compressed" /><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/Genetic+Algorithm/default.aspx">Genetic Algorithm</category></item><item><title>.Net 4.0 Reflection vs. Dynamics vs. Property Setting With Times</title><link>http://azurecoding.net/blogs/icbtw/archive/2009/06/26/net_5F00_4_5F00_0_5F00_reflection_5F00_vs_5F00_dynamics_5F00_vs_5F00_property_5F00_setting_5F00_with_5F00_times.aspx</link><pubDate>Fri, 26 Jun 2009 16:26:00 GMT</pubDate><guid isPermaLink="false">6e97e1e3-dc93-4c0d-b73f-e698f7d56013:1309</guid><dc:creator>David Justice</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/rsscomments.aspx?PostID=1309</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/commentapi.aspx?PostID=1309</wfw:comment><comments>http://azurecoding.net/blogs/icbtw/archive/2009/06/26/net_5F00_4_5F00_0_5F00_reflection_5F00_vs_5F00_dynamics_5F00_vs_5F00_property_5F00_setting_5F00_with_5F00_times.aspx#comments</comments><description>&lt;p&gt;
&lt;p&gt;Today I found my self hanging out after a sprint review with my partner in crime&amp;nbsp;&lt;a target="_blank" title="@sundriedcoder" href="http://twitter.com/sundriedcoder"&gt;Kevin Rohling&lt;/a&gt;. &amp;nbsp;TFS was down (not tfs&amp;#39;s fault, but some other freak accident...) and I hadn&amp;#39;t installed VS 2010 yet, so it seemed like the perfect time to do such things. I got everything up and running, and Kevin and I got into a discussion about Dynamics &amp;amp; performance. We made a quick lunch bet on which is faster, reflection or dynamics for simply setting a property. I figured reflection would be quicker, while Kevin thought dynamics would be faster. Well, I owe him lunch now. The code is below.&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/p&gt;...(&lt;a href="http://azurecoding.net/blogs/icbtw/archive/2009/06/26/net_5F00_4_5F00_0_5F00_reflection_5F00_vs_5F00_dynamics_5F00_vs_5F00_property_5F00_setting_5F00_with_5F00_times.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://azurecoding.net/aggbug.aspx?PostID=1309" width="1" height="1"&gt;</description><enclosure url="http://azurecoding.net/cfs-file.ashx/__key/CommunityServer.Components.PostAttachments/00.00.00.13.09/Program.zip" length="565" type="application/x-zip-compressed" /><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/.NET+4.0/default.aspx">.NET 4.0</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/new+cloud+app/default.aspx">new cloud app</category></item><item><title>MetaTable Expression Builder for Fetching Any Object Via Collection of Primary Keys</title><link>http://azurecoding.net/blogs/icbtw/archive/2008/11/04/metatable-expression-builder-for-fetching-any-object-via-collection-of-primary-keys.aspx</link><pubDate>Tue, 04 Nov 2008 10:53:00 GMT</pubDate><guid isPermaLink="false">6e97e1e3-dc93-4c0d-b73f-e698f7d56013:18</guid><dc:creator>David Justice</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/rsscomments.aspx?PostID=18</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://azurecoding.net/blogs/icbtw/commentapi.aspx?PostID=18</wfw:comment><comments>http://azurecoding.net/blogs/icbtw/archive/2008/11/04/metatable-expression-builder-for-fetching-any-object-via-collection-of-primary-keys.aspx#comments</comments><description>&lt;div style="font-family:Courier New;font-size:10pt;color:black;background:white;"&gt;
&lt;p style="margin:0px;"&gt;Basically, I wanted to build a method that gets an object from a data context whatever the primary key may look like, and whatever type that object may be. &amp;nbsp;I wrote this code a little while ago when I was exploring the DynamicData libs. &amp;nbsp;I was checking out some of the code in the libs, and noticed a very slick way one of devs was building expressions to grab back a data item based on a single key ID. &amp;nbsp;I thought the idea of building up the expression completely ROCKED, but I wanted to expand it out to include composite primary keys. &amp;nbsp;Below is my implementation of the composite primary key object fetcher. &amp;nbsp;Hopefully, it will make your life a little easier.&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;(Needed to grab the MetaModel class)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Web.DynamicData;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;(Create a new MetaModel for your Context or Contexts -- make sure they are only registered once!! &amp;nbsp;This can be an issue if you put this code in a static constructor, and you happen to have a generic class. &amp;nbsp;That would cause the code to get executed each time you specify a new generic argument for type construction (sry, I digress))&lt;/p&gt;
&lt;/div&gt;
&lt;div style="font-family:Courier New;font-size:10pt;color:black;background:white;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;Model = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MetaModel&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;Model.RegisterContext(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;([&lt;span style="color:#2b91af;"&gt;Your Context Type&lt;/span&gt;]));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;(Get your meta table based on the type from your registered context -- need the metadata about the table to figure out the keys)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="font-family:Courier New;font-size:10pt;color:black;background:white;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MetaTable&lt;/span&gt; GetMetaTable&amp;lt;T&amp;gt;()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;return&lt;/span&gt; dataModelMetaModel.GetTable(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (T));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;div style="font-family:Courier New;font-size:10pt;color:black;background:white;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;(This is where the heavy lifting occurs. &amp;nbsp;I commented what is going on. &amp;nbsp;There is not much more to say. &amp;nbsp;Enjoy!)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="font-family:Courier New;font-size:10pt;color:black;background:white;"&gt;
&lt;/div&gt;
&lt;div style="font-family:Courier New;font-size:10pt;color:black;background:white;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; T FetchObject&amp;lt;T&amp;gt;(&lt;span style="color:#2b91af;"&gt;MetaTable&lt;/span&gt; metatable, &lt;span style="color:#2b91af;"&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt; pks)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:blue;"&gt;class&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;IQueryable&lt;/span&gt; query = metatable.GetQuery();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;//build linq expression tree for where clause&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;ParameterExpression&lt;/span&gt; entityparam = &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.Parameter(metatable.EntityType, &lt;span style="color:#a31515;"&gt;&amp;quot;e&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;&amp;gt; internalExpressons = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt; equalexpression;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;MetaColumn&lt;/span&gt; member &lt;span style="color:blue;"&gt;in&lt;/span&gt; metatable.PrimaryKeyColumns)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;//convert primary key to proper type&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;PropertyInfo&lt;/span&gt; columnpropertyinfo = member.EntityTypeProperty;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;MemberExpression&lt;/span&gt; columnexpresson = &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.MakeMemberAccess(entityparam, columnpropertyinfo);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; internalExpressons.Add(&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.Equal(columnexpresson, &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.Constant(pks[member.Name])));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;// build up the primary key expressions&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (internalExpressons.Count &amp;gt; 1)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; equalexpression = &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.And(internalExpressons[0], internalExpressons[1]);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;for&lt;/span&gt; (&lt;span style="color:blue;"&gt;int&lt;/span&gt; i = 2; i &amp;lt; internalExpressons.Count; i++)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; equalexpression = &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.And(equalexpression, internalExpressons);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; equalexpression = internalExpressons[0];&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;// Create the where clause&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;LambdaExpression&lt;/span&gt; wherelambda = &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.Lambda(equalexpression, entityparam);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;MethodCallExpression&lt;/span&gt; wherecall = &lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;.Call(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Queryable&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Where&amp;quot;&lt;/span&gt;, &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {metatable.EntityType},&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.Expression, wherelambda);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = query.Provider.CreateQuery(wherecall);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;// Call FirstOrDefault to make sure the connection is closed when finished&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:green;"&gt;// saw some connections staying open when just grabbing the first object.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.Cast&amp;lt;T&amp;gt;().FirstOrDefault();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;a href="http://twitter.com/davidjustice"&gt;Twitter: @DavidJustice&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://azurecoding.net/aggbug.aspx?PostID=18" width="1" height="1"&gt;</description><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/MetaModel/default.aspx">MetaModel</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/Expressions/default.aspx">Expressions</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/Expression+Building/default.aspx">Expression Building</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/Linq/default.aspx">Linq</category><category domain="http://azurecoding.net/blogs/icbtw/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>