locked
How can I sort the selectlistitem based on the value RRS feed

  • Question

  • User-1355965324 posted

    I have the following Linq  Expression SQL. I want to sort the  list result based on the value. The value is coming

    '1' ',18','19','2','22','3'   I wan to sort  the the list as '1','2','3','18','19' and so on. The sorting sorder should be numerical order not alphabets order Please help

      public IEnumerable<SelectListItem> GetUserDepots(int UserID)   // For Dropdown
            {
                IEnumerable<SelectListItem> depotUserList = (from e in ctx.goUserDepartment
                                     join c in ctx.goDepot on e.DepotNo equals c.DepotNo
                                     join user in ctx.goUser
                                     on e.UserID equals user.UserID
                                     where e.UserID == UserID &&
                                     e.IsDeleted == false
                                     select new SelectListItem
                                     {
                                         Value = c.DepotNo.ToString(),
                                         Text = c.DepotName,
                                         Selected = user.DepotNo == e.DepotNo
                                     }).Distinct().ToList<SelectListItem>();
                return depotUserList;
            }

    Pol

    Tuesday, August 11, 2020 3:12 PM

Answers

  • User711641945 posted

    Hi polachan,

    Since your Value is already a string type, so directly Orderby is invalid, you should do like this:

    public IEnumerable<SelectListItem> GetUserDepots(int UserID)   // For Dropdown
    {
    
          IEnumerable<SelectListItem> depotUserList = (from e in ctx.goUserDepartment
                                     join c in ctx.goDepot on e.DepotNo equals c.DepotNo
                                     join user in ctx.goUser
                                     on e.UserID equals user.UserID
                                     where e.UserID == UserID &&
                                     e.IsDeleted == false
                                     select new SelectListItem
                                     {
                                         Value = c.DepotNo.ToString(),
                                         Text = c.DepotName,
                                         Selected = user.DepotNo == e.DepotNo
                                     }).Distinct().ToList<SelectListItem>();
    
            return depotUserList.OrderBy(a => a.Value.Length).ThenBy(a => a.Value).ToList();
    
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 3:20 AM
  • User991566988 posted

    Try This

    return depotUserList.OrderBy(a => Convert.ToInt32(a.Value));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 6:14 AM
  • User-1355965324 posted

    I applied the  linq as given below and it is working fine

    select new SelectListItem
     {
         Value = c.DepotNo.ToString(),
         Text = c.DepotName,
         Selected = user.DepotNo == e.DepotNo
     }).Distinct().ToList<SelectListItem>().OrderBy(x=>  int.Parse(x.Value));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 6:59 AM

All replies

  • User1120430333 posted
     return depotUserList.OrderBy(a => a.value);

    Selected = user.DepotNo == e.DepotNo

    What is the above statment about in a SelectListItem? Somehow that equates to true or false?

    Tuesday, August 11, 2020 3:27 PM
  • User-1355965324 posted

    That will not work. Because value is coming as text  . I tried but the value is coming as string like '1','12','13','2'

    Selected is an option to be selected value from the drop down in default

     Value = c.DepotNo.ToString(),
    Tuesday, August 11, 2020 4:02 PM
  • User475983607 posted

    polachan

    That will not work. Because value is coming as text  . I tried but the value is coming as string like '1','12','13','2'

    Selected is an option to be selected value from the drop down in default

     Value = c.DepotNo.ToString(),

    Use the openly published and well-known LINQ orderby.  polachan, it really helps to learn the basics and read the openly published documentation.  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/orderby-clause

    IEnumerable<SelectListItem> depotUserList = (from e in ctx.goUserDepartment
         join c in ctx.goDepot on e.DepotNo equals c.DepotNo
         join user in ctx.goUser
         on e.UserID equals user.UserID
         where e.UserID == UserID &&
         e.IsDeleted == false
         orderby c.DepotNo
         select new SelectListItem
         {
             Value = c.DepotNo.ToString(),
             Text = c.DepotName,
             Selected = user.DepotNo == e.DepotNo
         }).Distinct().ToList<SelectListItem>();

    Tuesday, August 11, 2020 4:39 PM
  • User1120430333 posted

    polachan

    That will not work. Because value is coming as text  . I tried but the value is coming as string like '1','12','13','2'

    Selected is an option to be selected value from the drop down in default

     Value = c.DepotNo.ToString(),

    You can use a Ternary operator on DeptNo length.

    https://www.dotnetperls.com/ternary

    Value = c.DepotNo.ToString().length() == 2 c.DepotNo.ToString() : '0' + c.DepotNo.ToString();

    The Selected is either true or false, and if it's true, the item is preselected in the dropdownlist.

    Tuesday, August 11, 2020 5:35 PM
  • User-1355965324 posted

    mgebhard,

    Before I am posting the question here , I did the same way you advised me . but that  is not working to me. 

    IEnumerable<SelectListItem> depotUserList = (from e in ctx.goUserDepartment
                                                             join c in ctx.goDepot on e.DepotNo equals c.DepotNo
                                                             join user in ctx.goUser
                                                             on e.UserID equals user.UserID
                                                             where e.UserID == UserID &&
                                                             e.IsDeleted == false
                                                             orderby c.DepotNo
                                                             select new SelectListItem
                                                             {
                                                                 Value = c.DepotNo.ToString(),
                                                                 Text = c.DepotName,
                                                                 Selected = user.DepotNo == e.DepotNo
                                                             }).Distinct().ToList<SelectListItem>();
                return depotUserList;

    Tuesday, August 11, 2020 10:25 PM
  • User711641945 posted

    Hi polachan,

    Since your Value is already a string type, so directly Orderby is invalid, you should do like this:

    public IEnumerable<SelectListItem> GetUserDepots(int UserID)   // For Dropdown
    {
    
          IEnumerable<SelectListItem> depotUserList = (from e in ctx.goUserDepartment
                                     join c in ctx.goDepot on e.DepotNo equals c.DepotNo
                                     join user in ctx.goUser
                                     on e.UserID equals user.UserID
                                     where e.UserID == UserID &&
                                     e.IsDeleted == false
                                     select new SelectListItem
                                     {
                                         Value = c.DepotNo.ToString(),
                                         Text = c.DepotName,
                                         Selected = user.DepotNo == e.DepotNo
                                     }).Distinct().ToList<SelectListItem>();
    
            return depotUserList.OrderBy(a => a.Value.Length).ThenBy(a => a.Value).ToList();
    
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 3:20 AM
  • User991566988 posted

    Try This

    return depotUserList.OrderBy(a => Convert.ToInt32(a.Value));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 6:14 AM
  • User-1355965324 posted

    I applied the  linq as given below and it is working fine

    select new SelectListItem
     {
         Value = c.DepotNo.ToString(),
         Text = c.DepotName,
         Selected = user.DepotNo == e.DepotNo
     }).Distinct().ToList<SelectListItem>().OrderBy(x=>  int.Parse(x.Value));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2020 6:59 AM