none
Order By Desc in LINQ RRS feed

  • Question

  • Hello,

    Can someone please tell me how I can order by LastName descending on this LINQ example:

    public List<Users> GetUserID(int user)
            {
                var usr = new List<Users>();
                using (ProdTrackEntities context = new ProdTrackEntities())
                {
                    usr = (from u in context.pt_User
                           select new Users
                           {
                               IDUser = u.IDUser,
                               LastName = u.LastName,
                               FirstName = u.FirstName
                           }).ToList();
                }
                return usr;
            }


    Wednesday, August 7, 2019 12:34 PM

Answers

  • It's pretty straight forward actually:

     usr = (from u in context.pt_User
            orderby u.LastName descending
            select new Users
            {
                 IDUser = u.IDUser,
                 LastName = u.LastName,
                 FirstName = u.FirstName
            }).ToList();

    The above query will order the records at the database side by translating it to the sql query of it, the following would also work but this order by will get applied in the memory not at db level:

     usr = (from u in context.pt_User
            select new Users
            {
                 IDUser = u.IDUser,
                 LastName = u.LastName,
                 FirstName = u.FirstName
            }).OrderByDescending(user => user.LastName)
              .ToList();


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by Ehsan Sajjad Wednesday, August 7, 2019 1:24 PM
    • Proposed as answer by Ehsan Sajjad Wednesday, August 7, 2019 1:33 PM
    • Marked as answer by Justair07 Wednesday, August 7, 2019 2:14 PM
    Wednesday, August 7, 2019 1:15 PM
  • Here is another method that uses lambda for the order by and is another way to write up the statement along with not loading children and not tracking these items.

    public List<Product> GetProducts()
    {
        using (var context = new NorthWindContext())
        {
    
            context.Configuration.AutoDetectChangesEnabled = false;
            context.Configuration.LazyLoadingEnabled = false;
    
            return (from product in context.Products.AsNoTracking()
                    select new Product()
                    {
                        ProductName = product.ProductName,
                        UnitPrice = 12.99M
                    })
                .OrderBy(p => p.ProductName)
                .ToList();
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Justair07 Wednesday, August 7, 2019 2:14 PM
    Wednesday, August 7, 2019 1:36 PM
    Moderator

All replies

  • It's pretty straight forward actually:

     usr = (from u in context.pt_User
            orderby u.LastName descending
            select new Users
            {
                 IDUser = u.IDUser,
                 LastName = u.LastName,
                 FirstName = u.FirstName
            }).ToList();

    The above query will order the records at the database side by translating it to the sql query of it, the following would also work but this order by will get applied in the memory not at db level:

     usr = (from u in context.pt_User
            select new Users
            {
                 IDUser = u.IDUser,
                 LastName = u.LastName,
                 FirstName = u.FirstName
            }).OrderByDescending(user => user.LastName)
              .ToList();


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Edited by Ehsan Sajjad Wednesday, August 7, 2019 1:24 PM
    • Proposed as answer by Ehsan Sajjad Wednesday, August 7, 2019 1:33 PM
    • Marked as answer by Justair07 Wednesday, August 7, 2019 2:14 PM
    Wednesday, August 7, 2019 1:15 PM
  • Here is another method that uses lambda for the order by and is another way to write up the statement along with not loading children and not tracking these items.

    public List<Product> GetProducts()
    {
        using (var context = new NorthWindContext())
        {
    
            context.Configuration.AutoDetectChangesEnabled = false;
            context.Configuration.LazyLoadingEnabled = false;
    
            return (from product in context.Products.AsNoTracking()
                    select new Product()
                    {
                        ProductName = product.ProductName,
                        UnitPrice = 12.99M
                    })
                .OrderBy(p => p.ProductName)
                .ToList();
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Justair07 Wednesday, August 7, 2019 2:14 PM
    Wednesday, August 7, 2019 1:36 PM
    Moderator
  • Perfect. Thank you very much!
    • Edited by Justair07 Wednesday, August 7, 2019 2:17 PM
    Wednesday, August 7, 2019 2:16 PM
  • Perfect. Thank you very much!
    Wednesday, August 7, 2019 2:17 PM