Spidey
Spidey

Reputation: 1613

linq orderby not sorted asc not working

I have the following information return in the grid, but it would not order the record first order by firstname then family name. Please let me know how i can achieve this. I am currently using MVC4 and kendoui grid

public ActionResult Index()
    {
        return View();
    }

public ActionResult GetSession([DataSourceRequest] DataSourceRequest request)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    return Json(GetAllSessionList().ToDataSourceResult(request),         JsonRequestBehavior.AllowGet);
} 
 private static IEnumerable<ListsViewModel> GetAllSessionList()
    {
        var context = new HEntities();

        return context.vwSessionListConfigs
            .OrderBy(x => x.FirstName).ThenBy(x => x.FamilyName)
            .Select(session => new ListsViewModel
            {
                ConsumerID = session.ConsID,
                ConsumerHCID = session.ConsHCL,
                ConsumerHRN = session.ConsHRK,
                ConsumerFirstName = session.FirstName,
                ConsumerFamilyName = session.GivenName,
                ConsumerGender = session.Gender,
            });
    }

View:

@(Html.Kendo().Grid<Web_App.ViewModel.ListsViewModel>()

    .Name("SList")
    .HtmlAttributes(new { @Style = "align:center; font-size:10px;" })
    .Columns(columns =>
    {
        columns.Bound(p => p.ConsID).Visible(false);
        columns.Bound(p => p.ConsHCL).Width(80);
        columns.Bound(p => p.ConsHRK).Width(50);
        columns.Bound(p => p.FirstName).Width(80);
        columns.Bound(p => p.GivenName).Width(80);
        columns.Bound(p => p.ConsumerAlias).Width(45);
        columns.Bound(p => p.Gender).Width(30);

        columns.Command(commands => commands.Edit()).Width(175);      
    })
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable()
    .Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .ServerOperation(true)
        .Model(model => model.Id(p => p.ConsID))
        .Read(read => read.Action("GetSession", "Manage").Type(HttpVerbs.Get))
        .Update("Edit", "Manage")         
    )
    .Events(events => events
                            .Change("change"))



)

Upvotes: 1

Views: 1584

Answers (1)

Petur Subev
Petur Subev

Reputation: 20213

It does not matter how you sort the collection before passing it to the ToDataSourceResult method. The ToDataSourceResult method will internally again sort the collection by the first property of the model.

To sort you collection initially you should use the Sort method of the dataSource.

.DataSource(dataSource => dataSource
            .Ajax()
            .Sort(st => {

                st.Add(m => m.Name).Descending();
                st.Add(m => m.PersonID).Descending(); 
            })

Upvotes: 3

Related Questions