<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>DDITDev : LINQ</title><link>http://blogs.msdn.com/dditweb/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>LINQ to SQL and multiple result sets in Stored Procedures</title><link>http://blogs.msdn.com/dditweb/archive/2008/05/06/linq-to-sql-and-multiple-result-sets-in-stored-procedures.aspx</link><pubDate>Wed, 07 May 2008 00:34:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8463979</guid><dc:creator>dditweb</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/dditweb/comments/8463979.aspx</comments><wfw:commentRss>http://blogs.msdn.com/dditweb/commentrss.aspx?PostID=8463979</wfw:commentRss><wfw:comment>http://blogs.msdn.com/dditweb/rsscomments.aspx?PostID=8463979</wfw:comment><description>&lt;p&gt;In this post I'm going to demonstrate how you would return and consume multiple result sets from a stored procedure in LINQ to SQL.&lt;/p&gt;  &lt;p&gt;Imagine you have a stored procedure like this one below. Very simple &amp;#8211; it&amp;#8217;s just returns all customers and all orders.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image001_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="231" alt="clip_image001" src="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image001_thumb.jpg" width="320" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In my LINQ to SQL Data Context, I have the following tables.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image002_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="159" alt="clip_image002" src="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image002_thumb.jpg" width="480" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As well as the stored procedure definition.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image003_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="66" alt="clip_image003" src="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image003_thumb.jpg" width="240" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Simple enough so far!&lt;/p&gt;  &lt;p&gt;The first thing we need to do is create a partial class and define a new method returning type &amp;#8216;IMultipleResults&amp;#8217;. I&amp;#8217;ve defined the one below to accept one parameter, which is a customerId.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image004_2.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="clip_image004" src="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image004_thumb.jpg" width="640" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You&amp;#8217;ll notice that there&amp;#8217;s a few attributes you need to add.&lt;/p&gt;  &lt;p&gt;1. Function &amp;#8211; this is the name of the stored procedure that will be called. It&amp;#8217;s the same one we defined in our data context above. Note: the &lt;i&gt;actual&lt;/i&gt; stored procedure name is irrelevant - it must be the name you have given the stored&amp;#160; procedure on the data context (which will be the same 99% of the time I&amp;#8217;d imagine.)&lt;/p&gt;  &lt;p&gt;2. ResultType &amp;#8211; This is the mapping that basically says &amp;#8220;Make the first result type of &amp;#8216;Customer&amp;#8217; and the second result type of &amp;#8216;Order&amp;#8217;&amp;#8221;.&lt;/p&gt;  &lt;p&gt;Once we&amp;#8217;ve defined this method, we can go ahead and consume it from our UI and/or OM as shown below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image005_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="202" alt="clip_image005" src="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/clip_image005_thumb.jpg" width="480" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As shown above, you call the method you defined, passing in a customer id in this case, which returns you a type of IMultipleResults. From there it&amp;#8217;s just a case of calling the &amp;#8216;GetResult&amp;#8217; method making sure to pass in the type of the object you want.&lt;/p&gt;  &lt;p&gt;The results are shown below!&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/image_4.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="259" alt="image" src="http://blogs.msdn.com/blogfiles/dditweb/WindowsLiveWriter/LINQtoSQLandmultipleresultsetsinStoredPr_CCC1/image_thumb_1.png" width="320" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I hope this helps!&lt;/p&gt;  &lt;p&gt;Jason&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8463979" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/dditweb/archive/tags/Jason/default.aspx">Jason</category><category domain="http://blogs.msdn.com/dditweb/archive/tags/SQL/default.aspx">SQL</category><category domain="http://blogs.msdn.com/dditweb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/dditweb/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>