Home  /  Questions  /  Question

108   96.7
Mar 29, 2012

Sorting with stored procedures

In the example for sql server they use inline statements and pass in the sortexpression and add it in the statement. If you are using stored procedures how do you use the passed in sortexpression for sorting the grid? the sortexpression being passed in may look like "weight asc" as you may know.

I do see you can add an .Orderby at the end of the return on the Db.Readlist but I'm not sure how that works.


public List<Audit> GetAudits(int id, string type, string code, string sortExpression)
            string proc = @"usr_StoredProc";
            object[] parms = {"@type", type, "@id", id, "@code", code};
            return Db.ReadList(proc, Make, parms);


280   99.8
Apr 03, 2012
Hi Dennis,

I'm not totally sure on this one, as I don't have more details about the Db-object, but I assume it's just a DataContext. In that case queries like e.g. ReadList() return an IEnumerable<T>. That means a OrderBy-statement after ReadList(proc, Make, parms) would just adjust your actual query string for a db-query (when it is actually done).

An example for a OrderBy is include in the sample-sourcecode below.


public class Contact
   int Id = 0;
   string Name = string.Empty;

var result = Db.ReadList(proc, Make, parms)
               .OrderBy(x => x.Name)           // add order statements to query
               .ToList();                      // execute actual query