<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DU8ARn89eip7ImA9WhRRFE4.&quot;"><id>tag:blogger.com,1999:blog-3281243809189348591</id><updated>2011-11-27T18:50:47.162-05:00</updated><category term="C#" /><category term="type safe include" /><category term="lambda" /><category term="Include" /><category term=".NET 4.0" /><category term="Entity Framework" /><category term="optional parameters" /><title>How do I...? .NET 3.5 / 4.0 SQL</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://howdoinetmw.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://howdoinetmw.blogspot.com/" /><author><name>M.W.</name><uri>http://www.blogger.com/profile/10411395970029695251</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/HowDoInet35/40Sql" /><feedburner:info uri="howdoinet35/40sql" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;A0ANQXk7eCp7ImA9WxBRGEQ.&quot;"><id>tag:blogger.com,1999:blog-3281243809189348591.post-8258459840157870614</id><published>2010-01-07T16:20:00.006-05:00</published><updated>2010-01-07T16:29:50.700-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-07T16:29:50.700-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET 4.0" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="optional parameters" /><title>Optional parameters in .NET 4.0</title><content type="html">If you're wondering how you can use optional parameters in .NET 4.0 - here's a brief starter post:&lt;br /&gt;&lt;br /&gt;When using optional parameters, you need to put your required parameters BEFORE the optional parameters.  You define a parameter as optional by assigning a value to it directly within the method signature:&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetCar(&lt;span class="kwrd"&gt;string&lt;/span&gt; maker, &lt;span class="kwrd"&gt;string&lt;/span&gt; color = &lt;span class="str"&gt;"red"&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt; year = &lt;span class="str"&gt;2009&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;     ...&lt;br /&gt;}&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Here maker is a required parameter, but color is optional with a &lt;span style="font-weight:bold;"&gt;default value&lt;/span&gt; of red.  If you don't specify anything else, it will default to red, and same for the year.  Optional parameters always need a default option.&lt;br /&gt;&lt;br /&gt;We can call it like this:&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;GetCar(&lt;span class="str"&gt;"Toyota"&lt;/span&gt;);  &lt;span class="rem"&gt;// returns a red 2009 toyota&lt;/span&gt;&lt;br /&gt;GetCar(&lt;span class="str"&gt;"Toyota"&lt;/span&gt;, &lt;span class="str"&gt;"blue"&lt;/span&gt;) &lt;span class="rem"&gt;//returns a blue 2009 toyota&lt;/span&gt;&lt;br /&gt;GetCar(&lt;span class="str"&gt;"Toyota"&lt;/span&gt;, &lt;span class="str"&gt;"blue"&lt;/span&gt;, 2010) &lt;span class="rem"&gt;// returns a blue 2010 toyota&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Aside from optional parameters, in .NET 4.0 you can specify the order of parameters to call in, or in the case of multiple optional parameters, specify values for certain ones by using ":".  Let's say in our above method, we just want to pass in the color, but use the default year.  We can call it like this:&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;GetCar(maker : &lt;span class="str"&gt;"Toyota"&lt;/span&gt;, color: &lt;span class="str"&gt;"blue"&lt;/span&gt;);&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Very useful.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3281243809189348591-8258459840157870614?l=howdoinetmw.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/CLR1KQprK_yDvByGQS6nYqxtYi0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CLR1KQprK_yDvByGQS6nYqxtYi0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/CLR1KQprK_yDvByGQS6nYqxtYi0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CLR1KQprK_yDvByGQS6nYqxtYi0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/HowDoInet35/40Sql/~4/onMQMoXBSzM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://howdoinetmw.blogspot.com/feeds/8258459840157870614/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://howdoinetmw.blogspot.com/2010/01/optional-parameters-in-net-40.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3281243809189348591/posts/default/8258459840157870614?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3281243809189348591/posts/default/8258459840157870614?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/HowDoInet35/40Sql/~3/onMQMoXBSzM/optional-parameters-in-net-40.html" title="Optional parameters in .NET 4.0" /><author><name>M.W.</name><uri>http://www.blogger.com/profile/10411395970029695251</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://howdoinetmw.blogspot.com/2010/01/optional-parameters-in-net-40.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A04DQX49cCp7ImA9WxBRGUg.&quot;"><id>tag:blogger.com,1999:blog-3281243809189348591.post-8817526729073341223</id><published>2009-12-30T09:18:00.007-05:00</published><updated>2010-01-08T09:12:50.068-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-08T09:12:50.068-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="lambda" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET 4.0" /><category scheme="http://www.blogger.com/atom/ns#" term="Entity Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="Include" /><category scheme="http://www.blogger.com/atom/ns#" term="type safe include" /><title>How do I create type safe includes with Entity Framework?</title><content type="html">A question you might come across is - "How do I include other entity sets when doing a query so as not to make multiple round trips to the database?  Calling ".Include" in your LINQ statements provides eager loading when retrieving objects to the database.  To take a step back: let's consider this scenario:&lt;br /&gt;&lt;br /&gt;You have two linked tables - "Car" and "Maker".  When you select a car object, you will have a linked entity called "Maker" and vice versa - if you select "Maker", you will have a linked entityset "Cars".  If you try calling one of these objects, EF will automatically make a call to the database for you to return the linked object.&lt;br /&gt;&lt;br /&gt;So you say, I don't want to do that!  I want to get the Car, and the Maker in one shot to the database.  You would think it'd be something like creating a join in the query, but not so.  You need to call ".Include" like so:&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (var db = &lt;span class="kwrd"&gt;new&lt;/span&gt; DatabaseEntities())&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; db.Cars.Include(&lt;span class="str"&gt;"Maker"&lt;/span&gt;).Where(c =&amp;gt; c.Color == &lt;span class="str"&gt;"Red"&lt;/span&gt;).FirstOrDefault();&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;or for query syntax:&lt;br /&gt;        &lt;div class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;return&lt;/span&gt; (from c &lt;span class="kwrd"&gt;in&lt;/span&gt; db.Cars.Include(&lt;span class="str"&gt;"Maker"&lt;/span&gt;) &lt;span class="kwrd"&gt;where&lt;/span&gt; c.Color equals &lt;span class="str"&gt;"Red"&lt;/span&gt; select c).FirstOrDefault();&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;The problem is we are passing a string into the Include.  This is bad. So I set out on creating a type safe Include extension.  Here it is:&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IQueryable&amp;lt;T&amp;gt; Include&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; IQueryable&amp;lt;T&amp;gt; mainQuery, Expression&amp;lt;Func&amp;lt;T, &lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; subSelector)&lt;br /&gt;        {&lt;br /&gt;            var selector = subSelector.Body &lt;span class="kwrd"&gt;as&lt;/span&gt; MemberExpression;&lt;br /&gt;            var expression = selector.Expression;&lt;br /&gt;            var includeString = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder();&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//Go through and insert parent expressions + "." (insert puts them before like prepend)&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;while&lt;/span&gt; (expression != &lt;span class="kwrd"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; expression.Type.Name != subSelector.Parameters[0].Type.Name)&lt;br /&gt;            {&lt;br /&gt;                includeString.Insert(0, &lt;span class="str"&gt;"."&lt;/span&gt;);&lt;br /&gt;                includeString.Insert(0, expression.Type.Name);&lt;br /&gt;                expression = ((MemberExpression)expression).Expression;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;//The actual name we were looking to get in the first place&lt;/span&gt;&lt;br /&gt;            includeString.Append(selector.Member.Name); &lt;span class="rem"&gt;//The base type we want to get&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; ((ObjectQuery&amp;lt;T&amp;gt;)mainQuery).Include(includeString.ToString());&lt;br /&gt;        }&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Basically we extend an IQueryable object.  These are, for the most part, our ObjectSets (mapped to database tables).  You pass in a lambda expression specifying the entity you want to retrieve eagerly, and it loops back from that and creates a string out of it.  You can use it like this:&lt;br /&gt;&lt;br /&gt;db.Cars.Include(c =&gt; c.Maker)&lt;br /&gt;&lt;br /&gt;You can also call several layers down like so:&lt;br /&gt;&lt;br /&gt;db.Cars.Include(c =&gt; c.Maker.Country)&lt;br /&gt;&lt;br /&gt;&lt;/t&gt;&lt;/t,&gt;&lt;/func&gt;&lt;/t&gt;&lt;/t&gt;&lt;/t&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3281243809189348591-8817526729073341223?l=howdoinetmw.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IAaKGmikBVFfADFFhrdAnUqzoBI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IAaKGmikBVFfADFFhrdAnUqzoBI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IAaKGmikBVFfADFFhrdAnUqzoBI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IAaKGmikBVFfADFFhrdAnUqzoBI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/HowDoInet35/40Sql/~4/CPgRom9gkQY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://howdoinetmw.blogspot.com/feeds/8817526729073341223/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://howdoinetmw.blogspot.com/2009/12/how-do-i-create-type-safe-includes-with.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3281243809189348591/posts/default/8817526729073341223?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3281243809189348591/posts/default/8817526729073341223?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/HowDoInet35/40Sql/~3/CPgRom9gkQY/how-do-i-create-type-safe-includes-with.html" title="How do I create type safe includes with Entity Framework?" /><author><name>M.W.</name><uri>http://www.blogger.com/profile/10411395970029695251</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://howdoinetmw.blogspot.com/2009/12/how-do-i-create-type-safe-includes-with.html</feedburner:origLink></entry></feed>

