<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>LINQ to SQL</title>
        <link>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/category/41.aspx</link>
        <description>LINQ to SQL</description>
        <language>en-US</language>
        <copyright>Shawn Weisfeld</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <item>
            <title>Call a Multiple-Rowset stored procedure with LINQ to SQL and without the designer</title>
            <link>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/12/call-a-multiple-rowset-stored-procedure-with-linq-to-sql.aspx</link>
            <description>&lt;p&gt;Multiple-Rowset allows you to return more than one select from your stored procedure. For example in the stored procedure below you can see we have 2 select statements.&lt;/p&gt;  &lt;p&gt;&lt;a href="file:///C:\Users\Shawn\AppData\Local\Temp\WindowsLiveWriter1286139640\supfiles6309CCF7\image3.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://drowningintechnicaldebt.com/shawnweisfeld/clip_image002_17D3F2F6.gif" width="551" height="547" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As we learned in my last 2 posts it is possible to use LINQ to SQL without the designer.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A simple select with LINQ to SQL and without the designer (&lt;a href="http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the-designer.aspx"&gt;http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the-designer.aspx&lt;/a&gt;) &lt;/li&gt;    &lt;li&gt;Call a stored procedure with LINQ to SQL and without the designer (&lt;a href="http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/call-a-stored-procedure-with-linq-to-sql-and-without-the-designer.aspx"&gt;http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/call-a-stored-procedure-with-linq-to-sql-and-without-the-designer.aspx&lt;/a&gt;) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;To utilize Multiple-Rowset we just need to tweak our function in our data context a little. &lt;/p&gt;  &lt;p&gt;&lt;a href="file:///C:\Users\Shawn\AppData\Local\Temp\WindowsLiveWriter1286139640\supfiles6309CCF7\image15.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://drowningintechnicaldebt.com/shawnweisfeld/clip_image004_3D5D7057.gif" width="775" height="344" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here you can see that we added 2 result type attributes. These map to our result sets. Since our stored procedure returns 2 result sets of customers we tell LINQ to SQL to mach each to the customer object. However if your stored procedure returns different types you can of course do that. We also changed our result type to that of IMultipleResults. This will allow us to pick off each of the result sets when we call our method.&lt;/p&gt;  &lt;p&gt;&lt;a href="file:///C:\Users\Shawn\AppData\Local\Temp\WindowsLiveWriter1286139640\supfiles6309CCF7\image11.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://drowningintechnicaldebt.com/shawnweisfeld/clip_image006_638FA3E2.gif" width="785" height="278" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here is the call to the method. You can see when we call “GetResult” the first time we get our first result set. Then when we call it a second time we get the second result set. &lt;/p&gt;  &lt;p&gt;&lt;a href="file:///C:\Users\Shawn\AppData\Local\Temp\WindowsLiveWriter1286139640\supfiles6309CCF7\image7.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://drowningintechnicaldebt.com/shawnweisfeld/clip_image008_17579A34.gif" width="652" height="519" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;That is it. &lt;/p&gt;&lt;img src="http://www.drowningintechnicaldebt.com/ShawnWeisfeld/aggbug/314.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>sweisfeld</dc:creator>
            <guid>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/12/call-a-multiple-rowset-stored-procedure-with-linq-to-sql.aspx</guid>
            <pubDate>Sun, 12 Jul 2009 21:45:23 GMT</pubDate>
            <wfw:comment>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/comments/314.aspx</wfw:comment>
            <comments>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/12/call-a-multiple-rowset-stored-procedure-with-linq-to-sql.aspx#feedback</comments>
            <wfw:commentRss>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/comments/commentRss/314.aspx</wfw:commentRss>
            <trackback:ping>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/services/trackbacks/314.aspx</trackback:ping>
        </item>
        <item>
            <title>Call a stored procedure with LINQ to SQL and without the designer</title>
            <link>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/11/call-a-stored-procedure-with-linq-to-sql-and-without.aspx</link>
            <description>&lt;p&gt;In my last post I talked about how to do a simple select with LINQ to SQL without the designer (&lt;a href="/blogs/shawnweisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the-designer.aspx" title="http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the-designer.aspx"&gt;http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the-designer.aspx&lt;/a&gt;). This is all well and good, but what if you need to use a stored procedure. Like this one. . . . &lt;/p&gt;
&lt;p&gt;&lt;a href="/shawnweisfeld/image_036499A1.png"&gt;&lt;img height="342" width="475" src="/shawnweisfeld/image_thumb_1F092597.png" alt="image" border="0" title="image" style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The first step is to add to our data context a method that maps to our stored procedure. You can see we start off with a Function attribute that tells LINQ to SQL what stored procedure to use. Additionally we decorate the parameters on our method with Parameter attributes that tell LINQ to SQL how to map the parameters. Then we just call our base classes ExecuteMethodCall passing along all the attributes (using the .NET reflection MethodInfo class), and our parameter. &lt;/p&gt;
&lt;p&gt;&lt;a href="/shawnweisfeld/image_5A2CDB95.png"&gt;&lt;img height="292" width="845" src="/shawnweisfeld/image_thumb_05053FA8.png" alt="image" border="0" title="image" style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Now we just need to use our new method.&lt;/p&gt;
&lt;p&gt;&lt;a href="/shawnweisfeld/image_38610304.png"&gt;&lt;img height="165" width="811" src="/shawnweisfeld/image_thumb_1C504419.png" alt="image" border="0" title="image" style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Here we can see we get the same results as we did in my last post. &lt;/p&gt;
&lt;p&gt;&lt;a href="/shawnweisfeld/image_5BADF7A9.png"&gt;&lt;img height="381" width="608" src="/shawnweisfeld/image_thumb_222AE7B2.png" alt="image" border="0" title="image" style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;In my next post we will use Multiple-Rowset to return multiple result sets from my stored procedure. &lt;/p&gt;&lt;img src="http://www.drowningintechnicaldebt.com/ShawnWeisfeld/aggbug/313.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>sweisfeld</dc:creator>
            <guid>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/11/call-a-stored-procedure-with-linq-to-sql-and-without.aspx</guid>
            <pubDate>Sun, 12 Jul 2009 03:15:00 GMT</pubDate>
            <wfw:comment>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/comments/313.aspx</wfw:comment>
            <comments>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/11/call-a-stored-procedure-with-linq-to-sql-and-without.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/comments/commentRss/313.aspx</wfw:commentRss>
            <trackback:ping>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/services/trackbacks/313.aspx</trackback:ping>
        </item>
        <item>
            <title>A simple select with LINQ to SQL and without the designer</title>
            <link>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the.aspx</link>
            <description>&lt;p&gt;Many folks poo-poo LINQ to SQL because they don’t like designers. On the other hand some developers, myself included, like knowing how things work behind the scenes, for those edge case moments when the designer cannot do something. The most common business case for this is if you want to LINQ to SQL-afy an existing suite of objects. Well using the designers is NOT a requirement of LINQ to SQL.&lt;/p&gt;  &lt;p&gt;Lets say for example you had an existing customer table and customer business object:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_1A0E037E.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_4EAE5FB9.png" width="458" height="180" /&gt;&lt;/a&gt; &lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_0039CD4F.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_4003B3D4.png" width="432" height="193" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Lets see if we can get LINQ to SQL to populate a list of customers from the database.&lt;/p&gt;  &lt;p&gt;Ok first we need to make sure we have a valid connection string.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_45DE576D.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_137A83EE.png" width="1135" height="166" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The next step is to create our DataContext. If you have used LINQ to SQL you will know that this guy does all the heavy lifting when it comes to getting data from and pushing data to the database.&lt;/p&gt;  &lt;p&gt;Creating the DataContext is simple. Just create a new class and set its parent class to DataContext. (You will probably need to add a reference to System.Data.Linq.)  Once you have the class created create a private static member called for the mapping source, and a constructor. The mapping source tells LINQ if it should look for attributes on the object, or in an xml configuration file for instructions on how to map the object model to the db model. This constructor will be chained to our base class passing up the mapping source and the connection string.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_79A64DBE.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_5CBD28E9.png" width="896" height="186" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p&gt;Since we selected an Attribute Mapping Source, lets go and decorate our customer class. using the attributes in the System.Data.Linq.Mapping namespace I tell LINQ to SQL what db table our object maps to and what columns each of the properties map to. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_77F581EA.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_3BC9B642.png" width="518" height="328" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The next step is to add a helper method to our Data Context exposing the data so we can easily query it.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_2FC7C60E.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_28A88996.png" width="425" height="167" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p&gt;Now we can just write our LINQ query, just like normal. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_7644B616.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_360E9C9C.png" width="601" height="231" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;That is it a simple LINQ to SQL query without the designer. Here are the results of the above query.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://drowningintechnicaldebt.com/shawnweisfeld/image_1991AABC.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://drowningintechnicaldebt.com/shawnweisfeld/image_thumb_75001D37.png" width="557" height="347" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In my next post I will kick this up a notch by using a stored procedure to get these same results. &lt;/p&gt;&lt;img src="http://www.drowningintechnicaldebt.com/ShawnWeisfeld/aggbug/312.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>sweisfeld</dc:creator>
            <guid>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the.aspx</guid>
            <pubDate>Sun, 12 Jul 2009 02:57:42 GMT</pubDate>
            <wfw:comment>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/comments/312.aspx</wfw:comment>
            <comments>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2009/07/11/a-simple-select-with-linq-to-sql-and-without-the.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/comments/commentRss/312.aspx</wfw:commentRss>
            <trackback:ping>http://www.drowningintechnicaldebt.com/ShawnWeisfeld/services/trackbacks/312.aspx</trackback:ping>
        </item>
    </channel>
</rss>
