locked
LINQ Order By column sort problem RRS feed

  • Question

  • User-2010267424 posted

    Hi,

     I have a LINQ Query:

    bikersList = (From c In ngBikersDataContext.Reg_Bikers _
                                Order By c.L_Name _
                                Select New Bikers() With { _
                                .BikerID = c.BikerID, _
                                .F_Name = c.F_Name, _
                                .M_Name = c.M_Name, _
                                .L_Name = c.L_Name, _
                                .MyID = c.MyID, _
                                .Site = c.Site.GetValueOrDefault, _
                                .bk_Building = c.bk_Building, _
                                .bk_City = c.bk_City, _
                                .bk_Zip = c.bk_Zip.GetValueOrDefault, _
                                .bk_Phone = c.bk_phone, _
                                .email = c.email, _
                                .DeptZone = c.DeptZone, _
                                .QuartID = c.QuartID.GetValueOrDefault, _
                                .BikerDays = c.BikerDays.GetValueOrDefault, _
                                .BikerMiles = c.BikerMiles.GetValueOrDefault, _
                                .BikerTime = c.BikerTime.GetValueOrDefault, _
                                .BKLockID = c.BKLockID.GetValueOrDefault, _
                                .bk_Start_DT = c.bk_Start_DT, _
                                .bk_End_DT = c.bk_End_DT, _
                                .bk_Quarter = c.bk_Quarter.GetValueOrDefault, _
                                .bk_Year = c.bk_Year.GetValueOrDefault, _
                                .bk_Comments = c.bk_Comments, _
                                .IsActive = c.IsActive.GetValueOrDefault _
                                }).ToList()

    This works great and sorts on L_Name. But I am trying to allow the user to sort the gridview themselves. So I am passing in the SortExpression as a string. But I don't know how to incorperate the SortExpression into the LINQ Query.

    I tried

    Order By c. & SortExpression

    But that did not work.

    Please help.

    Thank You

     

    Tuesday, March 29, 2011 4:55 PM

Answers

  • User-2005691517 posted

    Go to Dynamic SQL-like Linq OrderBy Extension

    Copy the class OrderByHelper shown there to your project

    Now you should be able to use it as yourList.OrderBy("propertyname") etc as explained in the post

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 1, 2011 6:09 AM

All replies

  • User-2005691517 posted

    Just google "linq dynamic orderby" and you will get what you are looking for (multiple ways in fact)

    Wednesday, March 30, 2011 2:00 AM
  • User-2010267424 posted

    Thank you for the response. I did just that and went through about 50 pages. Unfortunately none of them fit exactly what I am trying to do. I am new to all this so I am sure my ignorance played a big roll. But I could not figure it out. That is why I was hoping someone on here with more knowledge than myself could help me out.<o:p></o:p>

    Any help would be greatly appreciated.<o:p></o:p>

    Thank You<o:p></o:p>

     

    Wednesday, March 30, 2011 11:09 AM
  • User-2005691517 posted

    Go to Dynamic SQL-like Linq OrderBy Extension

    Copy the class OrderByHelper shown there to your project

    Now you should be able to use it as yourList.OrderBy("propertyname") etc as explained in the post

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 1, 2011 6:09 AM