I’m trying to sort a gridview with my objectdatasource which uses a function import (stored proc) from my entity model. The guide I was follow was from
http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control-part-3-sorting-and-filtering
Which has this code:
public IEnumerable<Department> GetDepartments(string sortExpression)
{
if (String.IsNullOrWhiteSpace(sortExpression))
{
sortExpression = "Name";
}
return context.Departments.Include("Person").OrderBy("it." + sortExpression).ToList();
}
How can I do an OrderBy if I’m calling a stored proc?
My code:
If String.IsNullOrWhiteSpace(sortExpression) Then
sortExpression = "Status"
End If
retReq = dataContext.usp_GetReport(Nothing, Nothing, Nothing, period, Nothing, Nothing, _
Nothing, Nothing, Nothing).ToList()
Any ideas? Thanks in advance
Update – Resolution
I’m sure that there’s a better way of making the sort functionality work in my case. But this is the method I did, it works but if there’s anyone that could help me simplify it, please let me know thanks.
Derived from msdn’s documentation http://msdn.microsoft.com/en-us/library/bb534966(v=vs.96).aspx#Y1200
If sortExpression IsNot Nothing Then
If sortExpression = "StatusDate DESC" Then
retReq = dataContext.usp_GetReport(Nothing, Nothing, Nothing, period, Nothing, Nothing, _
Nothing, Nothing, Nothing).OrderByDescending(Function(test As usp_GetReport_Result) test.StatusDate).ToList()
ElseIf sortExpression = "StatusDate" Then
retReq = dataContext.usp_GetReport(Nothing, Nothing, Nothing, period, Nothing, Nothing, _
Nothing, Nothing, Nothing).OrderBy(Function(test As usp_GetReport_Result) test.StatusDate).ToList()
End If
End If
Either
1) sort inside the stored-procedure, or
2) sort the returned data using Linq to objects.
e.g.
obviously 2) is going to sort the results which may not be what you require, if for instance your sproc is returning top(n).