Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble Passing Parameter to LinqToSql Stored Procedure
    primarykey
    data
    text
    <pre><code>public IEnumerable&lt;T&gt; ExecuteStoredProcedure&lt;T&gt;(params object[] parameters) { Type genericType = typeof(T); string commandthing = genericType.Name.Replace("Result", ""); //_db is my Linq To Sql database return _db.ExecuteQuery&lt;T&gt;(commandthing, parameters).AsEnumerable(); } </code></pre> <p>The stored procedure is named GetOrder and has a single int parameter of orderid. I'm calling the above like so:</p> <pre><code>SqlParameter parm1 = new SqlParameter("@orderid", SqlDbType.Int); parm1.Value = 123; var results = _session.ExecuteStoredProcedure&lt;GetOrderResult&gt;(parm1).Single(); </code></pre> <p>I'm receiving the following error: A query parameter cannot be of type 'System.Data.SqlClient.SqlParameter'</p> <p>Thoughts? Or am I just missing something obvious?</p> <p><strong>Update:</strong> I'm trying to make this as generic as possible...my current thinking is that I'm going to have to do some string trickery to create the ExecuteQuery text and parameters.</p> <p><strong>Update:</strong> Posting below my Session Interface and my Linq to Sql Implementation of the interface...hopefully that will clarify what I'm attempting to do</p> <pre><code> public interface ISession : IDisposable { void CommitChanges(); void Delete&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; expression) where T : class; void Delete&lt;T&gt;(T item) where T : class; void DeleteAll&lt;T&gt;() where T : class; T Single&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; expression) where T : class; IQueryable&lt;T&gt; All&lt;T&gt;() where T : class; void Add&lt;T&gt;(T item) where T : class; void Add&lt;T&gt;(IEnumerable&lt;T&gt; items) where T : class; void Update&lt;T&gt;(T item) where T : class; IEnumerable&lt;T&gt; ExecuteStoredProcedure&lt;T&gt;(params object[] parameters); } public class LinqToSqlSession : ISession { public readonly Db _db; public LinqToSqlSession() { _db = new Db(ConfigurationManager.ConnectionStrings[Environment.MachineName].ConnectionString); } public void CommitChanges() { _db.SubmitChanges(); } /// &lt;summary&gt; /// Gets the table provided by the type T and returns for querying /// &lt;/summary&gt; private Table&lt;T&gt; GetTable&lt;T&gt;() where T : class { return _db.GetTable&lt;T&gt;(); } public void Delete&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; expression) where T : class { var query = All&lt;T&gt;().Where(expression); GetTable&lt;T&gt;().DeleteAllOnSubmit(query); } public void Delete&lt;T&gt;(T item) where T : class { GetTable&lt;T&gt;().DeleteOnSubmit(item); } public void DeleteAll&lt;T&gt;() where T : class { var query = All&lt;T&gt;(); GetTable&lt;T&gt;().DeleteAllOnSubmit(query); } public void Dispose() { _db.Dispose(); } public T Single&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; expression) where T : class { return GetTable&lt;T&gt;().SingleOrDefault(expression); } public IEnumerable&lt;T&gt; ExecuteStoredProcedure&lt;T&gt;(params object[] parameters) { Type genericType = typeof(T); string commandstring = genericType.Name.Replace("Result", ""); //_db is my Linq To Sql database return _db.ExecuteQuery&lt;T&gt;(commandstring, parameters).AsEnumerable(); } public IQueryable&lt;T&gt; All&lt;T&gt;() where T : class { return GetTable&lt;T&gt;().AsQueryable(); } public void Add&lt;T&gt;(T item) where T : class { GetTable&lt;T&gt;().InsertOnSubmit(item); } public void Add&lt;T&gt;(IEnumerable&lt;T&gt; items) where T : class { GetTable&lt;T&gt;().InsertAllOnSubmit(items); } public void Update&lt;T&gt;(T item) where T : class { //nothing needed here } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload