<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.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>LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx</link><description>There's a lot of great stuff in .NET 3.5 and several different ways to work with LINQ technologies such as LINQ to SQL.&amp;#160; I'm currently putting together some demonstration code for a talk I'll be giving at DevConnections in Orlando and showing how</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Building an N-Layer ASP.NET Application with LINQ, Lambdas and Stored Procedures (Updated)</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5878525</link><pubDate>Thu, 28 Feb 2008 18:22:30 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5878525</guid><dc:creator>Community Blogs</dc:creator><author>Community Blogs</author><description>&lt;p&gt;Update: I refactored some of the code and also did a better job ensuring Dispose() is called everywhere&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5878525" width="1" height="1"&gt;</description></item><item><title>re: LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5821353</link><pubDate>Tue, 19 Feb 2008 15:21:25 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5821353</guid><dc:creator>dwahlin</dc:creator><author>dwahlin</author><description>&lt;p&gt;Dug,&lt;/p&gt;
&lt;p&gt;Thanks for commenting. &amp;nbsp;Yeah....the Lambda query is overly complex. &amp;nbsp;I was trying to mirror the LINQ query as closely as possible just to show what it would look like. &amp;nbsp;I should probably post what you list above as well since in reality people will be leveraging the data relationships in the object model. &amp;nbsp;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5821353" width="1" height="1"&gt;</description></item><item><title>re: LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5818128</link><pubDate>Tue, 19 Feb 2008 05:11:46 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5818128</guid><dc:creator>Dug</dc:creator><author>Dug</author><description>&lt;p&gt;Interesting post - Could you explain why are you using such complicated linq/lambda expressions. &amp;nbsp;Are you concerned about the sql that is being produced?&lt;/p&gt;
&lt;p&gt;I tried the following lambda expression and got the same result and a very similar sql &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;IEnumerable&amp;lt;OrderDescription&amp;gt; orders =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;db.Orders.Where(order =&amp;gt; order.OrderID == orderID).&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Join(db.OrderDetails, o =&amp;gt; o.OrderID, od =&amp;gt; od.OrderID,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;(o, od) =&amp;gt; new { ShipCompanyName = o.Shipper.CompanyName, od.ProductID, ProductName = od.Product.ProductName, Quantity = od.Quantity, UnitPrice = od.UnitPrice, SupplierName = od.Product.Supplier.CompanyName }).&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Select(o =&amp;gt; new OrderDescription&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Product = o.ProductName,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Quantity = o.Quantity,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ShipperName = o.ShipCompanyName,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Total = o.Quantity * o.UnitPrice,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UnitPrice = o.UnitPrice,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SupplierName = o.SupplierName&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;/p&gt;
&lt;p&gt;I also tried:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;List&amp;lt;OrderDescription&amp;gt; orders = new List&amp;lt;OrderDescription&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Order order = db.Orders.Where(o =&amp;gt; o.OrderID == orderID).Single&amp;lt;Order&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;foreach (OrderDetail od in order.OrderDetails)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; orders.Add(new OrderDescription&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Product = od.Product.ProductName,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Quantity = od.Quantity,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ShipperName = order.Shipper.CompanyName,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SupplierName = od.Product.Supplier.CompanyName,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Total = od.Quantity * od.UnitPrice,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UnitPrice = od.UnitPrice&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Which resulted in two additional sql query for each order detail to look up the product and the supplier. &amp;nbsp;This can be prevented by adding the following immediate loading directives to the method:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.Data.Linq.DataLoadOptions loadoptions = new DataLoadOptions();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;loadoptions.LoadWith&amp;lt;Order&amp;gt;(o =&amp;gt; o.OrderDetails);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;loadoptions.LoadWith&amp;lt;Order&amp;gt;(o =&amp;gt; o.Shipper);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;loadoptions.LoadWith&amp;lt;OrderDetail&amp;gt;(od =&amp;gt; od.Product);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;loadoptions.LoadWith&amp;lt;Product&amp;gt;(p =&amp;gt; p.Supplier);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;db.LoadOptions = loadoptions;&lt;/p&gt;
&lt;p&gt;This results in a some very ugly sql which seems to be reasonable similar to the original query. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I did not check any of the execution plans when comparing the sql that was produced. &amp;nbsp;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5818128" width="1" height="1"&gt;</description></item><item><title>Building an N-Layer ASP.NET Application with LINQ, Lambdas and Stored Procedures</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5817075</link><pubDate>Tue, 19 Feb 2008 01:20:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5817075</guid><dc:creator>Dan Wahlin's WebLog</dc:creator><author>Dan Wahlin's WebLog</author><description>&lt;p&gt;.NET 3.5 has a lot of great new features that can significantly enhance developer productivity.&amp;amp;#160;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5817075" width="1" height="1"&gt;</description></item><item><title>re: LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5815999</link><pubDate>Mon, 18 Feb 2008 21:44:51 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5815999</guid><dc:creator>dwahlin</dc:creator><author>dwahlin</author><description>&lt;p&gt;Damien,&lt;/p&gt;
&lt;p&gt;In cases where a developer is&amp;nbsp;contracting and shipping DB changes in a migration script then I would agree with you (having worked in that environment...which can definitely be painful at times) as long as the DB is SQL 2000/2005 of course.&amp;nbsp; In other cases where developers work directly at a company and have access to either the database or DBAs then I&amp;#39;d still prefer stored procedures there.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The nice thing is that it really doesn&amp;#39;t matter what I think since people can choose what&amp;#39;s best for their situation and LINQ gives them a lot of flexibility in that choice.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5815999" width="1" height="1"&gt;</description></item><item><title>re: LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5815140</link><pubDate>Mon, 18 Feb 2008 17:47:31 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5815140</guid><dc:creator>Damien Guard</dc:creator><author>Damien Guard</author><description>&lt;p&gt;I disagree that LINQ complicates application maintenance - if anything does it&amp;#39;s stored procedures.&lt;/p&gt;
&lt;p&gt;SP&amp;#39;s are often outside of the source control mechanism and complicate shipping upgrades because you then need a db migration script.&lt;/p&gt;
&lt;p&gt;Internally it means you can&amp;#39;t try changes locally without replicating the database environment or giving the SP a different name and remembering to rename both references before you go live... at the same time.&lt;/p&gt;
&lt;p&gt;Sure SP&amp;#39;s if there needs to be a lot of server-side processing or additional security though.&lt;/p&gt;
&lt;p&gt;[)amien&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5815140" width="1" height="1"&gt;</description></item><item><title>&amp;raquo; Daily Bits - February 18, 2008 Alvin Ashcraft&amp;#8217;s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5814219</link><pubDate>Mon, 18 Feb 2008 14:20:37 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5814219</guid><dc:creator>» Daily Bits - February 18, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.</dc:creator><author>» Daily Bits - February 18, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.</author><description>&lt;p&gt;Pingback from &amp;nbsp;&amp;amp;raquo; Daily Bits - February 18, 2008 Alvin Ashcraft&amp;amp;#8217;s Daily Geek Bits: Daily links, development, gadgets and raising rugrats.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5814219" width="1" height="1"&gt;</description></item><item><title>Weekly Link Post 29 &amp;laquo; Rhonda Tipton&amp;#8217;s WebLog</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5810997</link><pubDate>Mon, 18 Feb 2008 02:36:44 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5810997</guid><dc:creator>Weekly Link Post 29 « Rhonda Tipton’s WebLog</dc:creator><author>Weekly Link Post 29 « Rhonda Tipton’s WebLog</author><description>&lt;p&gt;Pingback from &amp;nbsp;Weekly Link Post 29 &amp;amp;laquo; Rhonda Tipton&amp;amp;#8217;s WebLog&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5810997" width="1" height="1"&gt;</description></item><item><title>re: LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5809296</link><pubDate>Sun, 17 Feb 2008 21:38:43 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5809296</guid><dc:creator>dwahlin</dc:creator><author>dwahlin</author><description>&lt;p&gt;Gregor,&lt;/p&gt;
&lt;p&gt;Glad the post was beneficial. &amp;nbsp;I agree with the &amp;quot;uneasy feeling&amp;quot; you mentioned. &amp;nbsp;We've been told for years to use stored procedures and avoid inline SQL but now LINQ tends to put things into more of a gray area. &amp;nbsp;But, people can choose what they like most which makes it cool.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5809296" width="1" height="1"&gt;</description></item><item><title>re: LINQ and Lambdas and Sprocs....Oh My!</title><link>http://weblogs.asp.net/dwahlin/archive/2008/02/17/linq-and-lambdas-and-sprocs-oh-my.aspx#5809281</link><pubDate>Sun, 17 Feb 2008 21:36:55 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5809281</guid><dc:creator>dwahlin</dc:creator><author>dwahlin</author><description>&lt;p&gt;Thanks for the comment Ryan. &amp;nbsp;Views would definitely be a good idea in this example especially for people that don't necessarily want stored procedures but yet want simplified LINQ or lambda queries. &amp;nbsp;I haven't taken the time to benchmark the three options...that would definitely be interesting though.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5809281" width="1" height="1"&gt;</description></item></channel></rss>