LINQ statement with parameter for Sorting

I have a jqGrid which loads data and I want to be able to sort by column - so here's my loading code after you clicked the column

Function Results() As JsonResult

        Dim sortColumn As String = (Request.Params("sidx")).ToString()
        Dim sortOrder As String = (Request.Params("sord")).ToString()

        Dim Sales = From m In db.SPAs
                    Order By what do i put here to have it sort by whatever column was passed to SortColumn?
                    Select m

Dim jsonData = New With { _
                .total = totalPages, _
                .page = pageIndex, _
                .records = totalRecords, _
                .rows = (From m In Sales _
                    Select New With { _
                        m.SPAID, m.CustomerID, m.VendorID, m.Salesman, m.IssueDate, m.ExpirationDate} _
Order By sortColumn
                ).ToList
                        }

        Return Json(jsonData, JsonRequestBehavior.AllowGet)

End Function
ProdigyOne2kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
I believe you're going to be limited to doing something like this:

From
Dim Sales = From m In db.SPAs
            Order By what do i put here to have it sort by whatever column was passed to SortColumn?
            Select m

Open in new window


To
Select Case sortColumn
    Case "SPAID"
        If sortOrder = "DESC" Then
            Sales = From m In Sales _
                    Order By m.SPAID Descending _
                    Select m
        Else
            Sales = From m In Sales _
                    Order By m.SPAID _
                    Select m
        End If
    Case "CustomerID"
        If sortOrder = "DESC" Then
            Sales = From m In Sales _
                    Order By m.CustomerID Descending _
                    Select m
        Else
            Sales = From m In Sales _
                    Order By m.CustomerID _
                    Select m
        End If
    Case "VendorID"
        If sortOrder = "DESC" Then
            Sales = From m In Sales _
                    Order By m.VendorID Descending _
                    Select m
        Else
            Sales = From m In Sales _
                    Order By m.VendorID _
                    Select m
        End If
    Case "Salesman"
        If sortOrder = "DESC" Then
            Sales = From m In Sales _
                    Order By m.Salesman Descending _
                    Select m
        Else
            Sales = From m In Sales _
                    Order By m.Salesman _
                    Select m
        End If
    Case "IssueDate"
        If sortOrder = "DESC" Then
            Sales = From m In Sales _
                    Order By m.IssueDate Descending _
                    Select m
        Else
            Sales = From m In Sales _
                    Order By m.IssueDate _
                    Select m
        End If
    Case "ExpirationDate"
        If sortOrder = "DESC" Then
            Sales = From m In Sales _
                    Order By m.ExpirationDate Descending _
                    Select m
        Else
            Sales = From m In Sales _
                    Order By m.ExpirationDate _
                    Select m
        End If
End Select

Open in new window

0
Fernando SotoRetiredCommented:
Hi ProdigyOne2k;

You can build your query in two parts. The second part being a dynamic created OrderBy expression treee which gets combind with the normal query. See the code snippet.

Function Results() As JsonResult

    Dim qSales As IQueryable = From m In db.SPAs _
                               Select m
    
    ' The column to order on    
    Dim columnToOrderBy As String = (Request.Params("sidx")).ToString()

    ' Create the OrderBy part of the Expression tree an attach to qSales
    Dim salesParam As ParameterExpression = Expression.Parameter(GetType(SPAs), "m")
    Dim member As MemberExpression = Expression.PropertyOrField(salesParam, columnToOrderBy)
    Dim lambda As LambdaExpression = Expression.Lambda(member, salesParam)
    Dim exprArgTypes As Type() = {qSales.ElementType, lambda.Body.Type}
    Dim methodCall As MethodCallExpression = Expression.Call(GetType(Queryable), "OrderBy", exprArgTypes, qSales.Expression, lambda)
    Dim salesQuery As IQueryable = qSales.Provider.CreateQuery(methodCall)    
                
    ' In the query below do your select clause 
    Dim jsonData = New With { _
                       .total = totalPages, _
                       .page = pageIndex, _
                       .records = totalRecords, _
                       .rows = (From m In salesQuery.Cast(Of SPAs)()
                                Select New With { _
                                    m.SPAID, _
                                    m.CustomerID, _
                                    m.VendorID, _
                                    m.Salesman, _
                                    m.IssueDate, _
                                    m.ExpirationDate _
                                }).ToList()
                        }

        Return Json(jsonData, JsonRequestBehavior.AllowGet)

End Function 

Open in new window


Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi ProdigyOne2k;

BTW you will need this imports statement in your code file.to use my code in my last post.

Imports System.Linq.Expressions

Fernando
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

käµfm³d 👽Commented:
@FernandoSoto

I had a feeling you'd pop in here and school me  ; )
0
Fernando SotoRetiredCommented:
@kaufmed

It's a new one I just learned and then saw the question and had to post. Isn't learning a great thing?
0
Fernando SotoRetiredCommented:
@kaufmed

I see you went to FSU I went to UCF.
0
käµfm³d 👽Commented:
@FernandoSoto

I'm still @ FSU  = )
0
Fernando SotoRetiredCommented:
@FernandoSoto

Well then keep up the good works and dedication.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.