Back to list
Views:   7.2K
Replies:  1

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);

Dennis Tucker, Mar 29, 2012
Reply 1
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

Florian Schaeffler, Apr 03, 2012
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.