locked
Order bY Clause Being Ignored in linq RRS feed

  • Question

  • User442275730 posted

    My Order by clause is being ignored so it is the following is the query I am using to retrieve the days.

            public List<StandardLookup> GetDaysOfWeek()
            {
                List<StandardLookup> lookups = new List<StandardLookup>();
    
                try
                {
                    var q = from lookup in apertureNetEntities.tblApertureNetLookUps.Where(a => a.lookup_type == Constants.daysOfWeek).OrderBy(o => o.colOrder)
                            orderby lookup.lookup_description
                            select new
                            {
                                LookLookupValue = lookup.lookup_Code,
                                LookupDescription = lookup.lookup_description.Trim()
                            };
    
                    if (q != null)
                    {
                        Array.ForEach(q.ToArray(), l =>
                        {
                            lookups.Add(new StandardLookup(l.LookLookupValue, l.LookupDescription));
                        });
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("Error in GetDaysOfWeek function aperturenetdal " + ex.InnerException.ToString());
                }
    
                return lookups;
            }

    Even though this sql works

    /****** Script for SelectTopNRows command from SSMS  ******/
    SELECT TOP 1000 [lookup]
          ,[lookup_type]
          ,[lookup_Code]
          ,[lookup_description]
          ,[lookup_email]
          ,colOrder
          ,[notes]
          ,[isEnabled]
          ,[createDate]
          ,[authUserName]
          ,[authCreatedDate]
          ,[isDeleted]
      FROM [aperturenet].[dbo].[tblApertureNetLookUps]
      where lookup_type='B49C1C37-C8D6-4778-B576-196FF12A8E4D'
      order by colOrder ASC

    Monday, March 21, 2016 9:56 AM

Answers

  • User364480375 posted

    david us r using 2 times orderby clause

     var q = from lookup in apertureNetEntities.tblApertureNetLookUps.Where(a => a.lookup_type == Constants.daysOfWeek).OrderBy(o => o.colOrder)
                            orderby lookup.lookup_description

    try to use distinct clause in

    colOrder

     then order by clause in 

    lookup.lookup_description
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 21, 2016 10:17 AM