Frank Myat Thu
Frank Myat Thu

Reputation: 4474

Generic List Object OrderBy dynamic Column name

I am trying to sort data which is from Generic List object.
By using below code, I can sort my data according to Title column.
But what I would like to do is I would like to sort data according to parameter value called sidx.

public ActionResult ListingGridData(string sidx, string sord, int page, int rows)
    {
        int currentPage = Convert.ToInt32(page) - 1;
        int totalRecords = 0;
        var SeminarList = (List<Seminar>)null;

        if(sord.Equals("asc"))
            SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
                  .OrderBy(x => x.Title )
                  .Skip(currentPage * rows)
                  .Take(rows)
                  .ToList();
        else
            SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
                  .OrderByDescending(x => x.Title)
                  .Skip(currentPage * rows)
                  .Take(rows)
                  .ToList();

        totalRecords = seminarRepository.AllSeminarList().Seminar_Masters.Count;

        var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);

        ....
    }

So I have modified my code like that, but it does not work.
It does not sort as I have expected.

SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
                  .OrderBy(x => sidx)
                  .Skip(currentPage * rows)
                  .Take(rows)
                  .ToList();

The object which I now use is pure List object, not IQueryable object.

As for sorting purpose, I don't want to go back to SQL select statement, I would like to sort data at Controller layer.

Please let me get any suggestions.

Upvotes: 1

Views: 4602

Answers (3)

Justin Pihony
Justin Pihony

Reputation: 67115

You must order by a column that is in your result set. If the sidx is not part of your resultset then it is like ordering by a constant. Thus every row will be the same and it will not REALLY be ordered. Let's assume sidx's value is 10, then it would be like ordering this resultset:

Row 1 | 10
Row 2 | 10
Row 3 | 10
...

You can see how the ordering would be pointless

What you need to do is add sidx to your result set if that is what you want:

SeminarList = seminarRepository.AllSeminarList().Seminar_Masters
                  .OrderBy(x => x.sidx)
                  .Skip(currentPage * rows)
                  .Take(rows)
                  .ToList();

Upvotes: 2

StriplingWarrior
StriplingWarrior

Reputation: 156624

So you're saying that the calling method will provide a Property name via the sidx parameter, and you want to sort by that property?

The easiest way is to use the Dynamic LINQ library. Another option would be to use reflection to find the property you're looking for, then build a lambda expression from it.

Upvotes: 1

Pete_ch
Pete_ch

Reputation: 1321

See answer for dynamic where and orderby using some helper methods here where can I find a good example of using linq & lambda expressions to generate dynamic where and orderby sql?

Upvotes: 1

Related Questions