none
LINQ orderby confusion RRS feed

  • Question

  • First, where's the forum for this?

    I have this LINQ query against an my entity model, and I can't figure out why the orderBy clause isn't having any effect. However, If I uncomment the OrderByDescending method at the end of the query, then it does work.

    GridView1.DataSource = (
                         from RLK in db.RegSerialNumber
                         from RPV in db.RegProductVer
                         orderby RLK.dtCreated descending
                         where true
                         select new
                        { 
                             SerialNum = RLK.iSerialNumber,
                             Date = RLK.dtCreated,
                             Customer = RLK.RegCustomer.RegContact.sFirstName
                                        + " " +
                                        RLK.RegCustomer.RegContact.sLastName,
                             // Product = RPV.sProductShortDescr,    
                             Allowed = RLK.iQtyAllowed,
                             Installed = (int?)RLK.RegComputerKey.Sum(x => x.bInstalled ? 1 : 0)
                         });//.OrderByDescending(x=>x.Date).Take(200);




    • Moved by OmegaManModerator Monday, April 27, 2009 6:50 PM (From:Visual C# Language)
    Friday, April 24, 2009 10:35 PM

Answers

All replies

  • Hmmm. I messed around with this a little and I had no problems with it. I did have to change it to use my business objects since I did not have yours. This is my code and it worked without issue:

                UnboundGrid.DataSource = (
                         from RLK in Customers.Retrieve()
                         from RPV in Employees.Retrieve()
                         select new
                         {
                             SerialNum = RLK.iSerialNumber,
                             Date = RLK.dtCreated,
                             Customer = RLK.sFirstName
                                        + " " +
                                        RLK.sLastName,
                             Employee = RPV.sFirstName
                                        + " " +
                                        RPV.sLastName,
                         }).OrderByDescending(x=>x.Date).Take(200);
    When you said it does not work, was it working and just not sorting?
    www.insteptech.com
    Saturday, April 25, 2009 12:38 AM
  • It's not sorting when the orderby clause is embedded in the middle of the linq statement. When it's written with the orderbydescending function at the end, then it is sorting.
    Monday, April 27, 2009 5:22 PM
  • Hi,

    Its doesnt matter where you put your "orderby" clause. Its where you put your "from" clause. I think RPV should precede RLK if it needs to sort without the
    .OrderByDescending(x=>x.Date).Take(200);

    I didnt do much reasoning but I think this might be the behavior of LINQ.

    GridView1.DataSource = (
                         from RPV in db.RegProductVer
                         orderby RLK.dtCreated descending
                         where true
                         from RLK in db.RegSerialNumber
                         select new
                        {
                             SerialNum = RLK.iSerialNumber,
                             Date = RLK.dtCreated,
                             Customer = RLK.RegCustomer.RegContact.sFirstName
                                        + " " +
                                        RLK.RegCustomer.RegContact.sLastName,
                             // Product = RPV.sProductShortDescr,   
                             Allowed = RLK.iQtyAllowed,
                             Installed = (int?)RLK.RegComputerKey.Sum(x => x.bInstalled ? 1 : 0)
                         });//.OrderByDescending(x=>x.Date).Take(200);

    I was able to simulate this with the below sample code.

     class SampleClass
      {
                public int Age{ get; set; }
                public string Name { get; set; }
      }


            private void Sort()
            {
                List<SampleClass> list = new List<SampleClass>();

                Random random = new Random();

                for (int i = 0; i < 10; i++)
                {
                    SampleClass sam = new SampleClass() { Age = random.Next(), Name = "Hi_" + i.ToString() };
                    list.Add(sam);
                }

                var orderedList = from l in list
                                  orderby l.Age descending
                                  where true
                                  from l1 in list
                                  select new { AGE = l1.Age, NAME = l1.Name };
     }

    Hope this helps.

    Monday, April 27, 2009 6:05 PM
  • What does the produced query look like?   If you have the orderby right after the from clauses, does the generated SQL query have no order by clause or does it just seem to give you the results in the wrong order?


    Wayward LINQ Lacky
    Tuesday, April 28, 2009 10:29 PM
    Moderator

  • Hi,

    You got the answer for your second post.
    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/7b8cabd2-f2f7-4413-b611-6234531c565f

    Please don't duplicate the post.

    Thanks and Regards,
    Vijay Pandurangan
    Wednesday, April 29, 2009 12:47 PM