views:

43

answers:

1

I seem to be having issues with getting my jqgrids working well with linq to sql in my asp.net mvc project.

The issue I am having is correctly using the sidx and sord parameters. Most resources I find say to do something like

var questions = context.Questions
  .OrderBy(sidx + " " + sord)
  .Skip(pageIndex * pageSize)
  .Take(pageSize);

In Visual Studio 2010 (.net 4 project, MVC2) I get a compile error on the order by because it seems there is no linq orderby extension that takes just a string as a parameter, they all want a delegate/lamda.

How can I implement ordering into my app so my grids can sort properly by columns?

A: 

you can make method that translates the strings into lambda expressions. I've not tested this but it could be like this:

private Expression<Func<Person, T>> orderbyExpression(string column)
{
    switch (column)
    {
       case: "Name":
           return p => p.Name;

       case: "Sex":
          return p => p.Sex;
    }

}

you'll have to sort out the asc/desc setting seperately as well

Jonny Cundall
I was hoping for something a bit more scalable :/
KallDrexx
I suppose you could try using reflection to get the properties in each case, although that might not work because Linq is pretty picky about what expressions it will accept
Jonny Cundall