<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Plop Central: LINQ to SQL with dynamically generated Stored Prcedure</title>
    <link>http://www.plopcentral.com/articles/2008/02/20/linq-to-sql-with-dynamically-generated-stored-prcedure</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Yeah, right!</description>
    <item>
      <title>LINQ to SQL with dynamically generated Stored Prcedure</title>
      <description>&lt;p&gt;I&amp;#8217;ve been using &lt;span class="caps"&gt;LINQ&lt;/span&gt; to &lt;span class="caps"&gt;SQL&lt;/span&gt; which is a very nice feature for .NET. It allows you to very quickly model your database objects as objects in code and return them as strongly typed .NET objects.&lt;/p&gt;


	&lt;p&gt;Seems to have &amp;#8216;nicked&amp;#8217; (been inspired by?) quite a few ideas from ActiveRecord for Ruby (and Rails). In fact a lot of the new language features (e.g. predicates as anonymous functions for selection, and iterators) seem to borrow from things learnt by being exposed to dynamic languages. All in all it makes .NET much more fun to work in.&lt;/p&gt;


	&lt;p&gt;Anyway, I was using all this goodness in a project (VS.NET 2008 Orcas beta) and had all my Entities modelled and could use &lt;span class="caps"&gt;LINQ&lt;/span&gt; to query and transform my entities programmatically. I also had a few Stored procedures mapped as methods of the data context. I ran into a problem when I wanted to generate some &lt;span class="caps"&gt;SQL&lt;/span&gt; on the fly so that users could get adhoc filters of their calls.&lt;/p&gt;


	&lt;p&gt;I didn&amp;#8217;t want to drop out into old style &lt;span class="caps"&gt;ADO&lt;/span&gt;.NET using my DBHandler as i would lose the strong typing etc. I decided to use a QueryReport table (as I&amp;#8217;ve done numerous times before) to allow me to do ad-hoc queries without being open to &lt;span class="caps"&gt;SQL&lt;/span&gt; Injection. The table consisted of an ID, Name and &lt;span class="caps"&gt;SQL&lt;/span&gt; field. My Stored procedure could take an ID parameter, look up the &lt;span class="caps"&gt;SQL&lt;/span&gt; and use it to filter the calls table&lt;/p&gt;


e.g.
&lt;pre&gt;
ID           Name                SQL
1            All calls           Select * from Calls Where 1 = 1
2            Open calls          Select * from Calls Where Status = 1
3            Old calls           Select * from Calls Where TimeStamp &amp;lt; DateAdd(d, -14,GetDate())
&lt;/pre&gt;

My stored procedure looked like this 
&lt;pre&gt;
ALTER PROCEDURE [dbo].[spExecStoredQuery] 
    @QueryReportID int = 0
AS
BEGIN
    SET NOCOUNT ON;
    declare @queryText varchar(500)
    select 
        @QueryText = SQL 
    from 
        QueryReports
    where
        id = @QueryReportID

    exec (@querytext)
END
&lt;/pre&gt;

	&lt;p&gt;The sp always returned All fields from the Calls table, so I should be able to map it to a Call object in the &lt;span class="caps"&gt;LINQ&lt;/span&gt; designer. I created the stored procedure and went about adding it as a method to the &lt;span class="caps"&gt;LINQ&lt;/span&gt; designer.&lt;/p&gt;


	&lt;p&gt;My problem occurred when I went to set it&amp;#8217;s return value to be of type Call. It wouldn&amp;#8217;t let me. The option was disabled and greyed out. I immediately assumed that it was because I had an Exec statement in the stored procedure, and assumed it was either a security issue or the designer could not work out it&amp;#8217;s return type. I then removed all the exec code from sp and had it return a straight &lt;em&gt;select * from calls&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;This still didn&amp;#8217;t work. I then renamed the Stored procedure to spStoredQuery. I added it to the designer and I was then able to set its &lt;em&gt;Return Type&lt;/em&gt; to Call. This lead me to believe that the designer didn&amp;#8217;t like the &amp;#8216;exec&amp;#8217; in the name of the Stored Procedure. In order to test this I renamed it back to spExecStoredQuery, but, it still worked okay.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve since played around with different versions and names and have discovered the following&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;The first time you add the sp as a method to the designer it checks for return values to try and guess the output type.&lt;/li&gt;
		&lt;li&gt;If it cannot guess the type, it will not let you set a return type in the designer at all&lt;/li&gt;
		&lt;li&gt;If you delete the method then fix the stored procedure so that it returns a constant return type, the designer caches the previous type and will still not let you set a return type.&lt;/li&gt;
		&lt;li&gt;If you rename the stored procedure and re-add it, it checks again, sees a return type (doesn&amp;#8217;t matter what it is) This allows you to manually select a return type in the designer.&lt;/li&gt;
		&lt;li&gt;If you then remove the non-dynamic select, and make the stored procedure dynamic, the method still works, and coerces the return type to be of type you selected.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So, the outcome of all this is: If you are dynamically generating a query in a stored procedure and want to add it as a method with a return type in the &lt;span class="caps"&gt;LINQ&lt;/span&gt; designer, add it first with non-dynamic select (select * from foo), override it&amp;#8217;s return type if required, then add the dynamic &lt;span class="caps"&gt;SQL&lt;/span&gt; to stored procedure and you&amp;#8217;re laughing.&lt;/p&gt;</description>
      <pubDate>Wed, 20 Feb 2008 13:38:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9803ceca-3ae9-43b7-89e7-ee9bcf898a00</guid>
      <author>Paul McConnon</author>
      <link>http://www.plopcentral.com/articles/2008/02/20/linq-to-sql-with-dynamically-generated-stored-prcedure</link>
      <category>Coding</category>
      <category>LINQ</category>
      <category>stored</category>
      <category>procedure</category>
      <category>Gotcha</category>
      <category>.NET</category>
    </item>
    <item>
      <title>"LINQ to SQL with dynamically generated Stored Prcedure" by KFrancis</title>
      <description>I've tried to do this, but I'm getting "Specified cast is not valid." exceptions when I try to call the stored procedure.
This solution fit my issue perfectly until I got those casting exceptions.

Any ideas?</description>
      <pubDate>Wed, 23 Jul 2008 20:54:32 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:fed78252-89b8-4f44-878b-dac83aa2e6bc</guid>
      <link>http://www.plopcentral.com/articles/2008/02/20/linq-to-sql-with-dynamically-generated-stored-prcedure#comment-2787</link>
    </item>
    <item>
      <title>"LINQ to SQL with dynamically generated Stored Prcedure" by elvedrano</title>
      <description>Easy way:
You can open the .dbml file with the XML editor and edit the return type there - that is the file from which the .designer.cs code is auto generated from. The .designer.cs file will be automagically generated correctly this way.

Hard way:
Do not use drag-drop to add your SP to the DBML, instead go to codebehind and add the call manually as a new partial method.

</description>
      <pubDate>Fri, 20 Jun 2008 12:33:53 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:e7210a9b-ae79-4961-b20c-1b3e3db247ad</guid>
      <link>http://www.plopcentral.com/articles/2008/02/20/linq-to-sql-with-dynamically-generated-stored-prcedure#comment-2772</link>
    </item>
    <item>
      <title>"LINQ to SQL with dynamically generated Stored Prcedure" by Paulie</title>
      <description>Jayshree, once you have used the designer to auto-generate the code for the stored procedure, you can move the code out of the .designer.vb or .designer.cs code into your own partial class.

In Visual Studio, go to Show All Files, expand the .dbml file, you should see a .layout and a code file. 

Open the code file and find the code that declares your data context (Partial Public Class FooDataContext) 

Copy the whole datacontext code into a new code file. In the new code file, remove everything except the stored procedure call declarations, and their return types (ie you don't need Public sub New etc as this is a partial class).


This means that when your designer is regenerated, your declaration of your stored procedure remains intact!


P</description>
      <pubDate>Mon, 21 Apr 2008 09:07:12 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:600370f3-918d-489f-a903-33a5cbd61552</guid>
      <link>http://www.plopcentral.com/articles/2008/02/20/linq-to-sql-with-dynamically-generated-stored-prcedure#comment-2769</link>
    </item>
    <item>
      <title>"LINQ to SQL with dynamically generated Stored Prcedure" by Jayshree</title>
      <description>Is there any way out for above scenario.Because though we add anything in designer.cs of Linq file, the next time when we drag &amp; drop anything on to it,the custom code get removed.Can we change this file dynamically?</description>
      <pubDate>Tue, 15 Apr 2008 11:22:15 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:09724ba3-d361-4c88-9db2-d5b3366124e9</guid>
      <link>http://www.plopcentral.com/articles/2008/02/20/linq-to-sql-with-dynamically-generated-stored-prcedure#comment-2768</link>
    </item>
  </channel>
</rss>
