locked
OrderBy DayOfWeek help.... RRS feed

  • Question

  • I receive the following error when I try to do an orderby.

    The argument 'value' was the wrong type. Expected 'System.Int32'. Actual 'System.Object'.

    //Get receiving hours
                var test = from r in data.ReceivingHours
                           let i = (int)(DayOfWeek)Enum.Parse(typeof(DayOfWeek), r.DayType.Name)
                           where r.EntityType.Name.ToLower().Trim() == "shipto"
                           && r.EntityID == order.CustomerShipTo.ID
                           orderby i descending
                           select r;
    I want to order by the days of the week, Sunday, Monday.... I used i to get the index for DayOfWeek type which is in the right order. Any help would be appreciated, thanks.
    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Friday, July 10, 2009 2:42 PM

Answers

  • I'm sorry, I can't tell here.  The code you posted doesn't reproduce the error.  It properly orders the days.  I got the following output:

    Friday
    Thursday
    Monday
    Sunday

    My guess is that your error is from somewhere else in your code.  Try iterating through the whole thing, or calling ".ToList()" on test immediately afterwards.  If the application crashes, post the stack trace and error message here.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by 01000011 Friday, July 10, 2009 6:36 PM
    Friday, July 10, 2009 6:29 PM
    Moderator

All replies

  • Hmmm... I think we're going to need more context.  In my limited test, it seems fine.


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Friday, July 10, 2009 2:51 PM
    Moderator
  • It dosent throw an exception until I look at it through Quick Watch. It there a better approach to what I am doing?
    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Friday, July 10, 2009 3:10 PM
  • Not sure, like I said... it looks fine to me from my limited perspective.  What do you get when you hover over "r.DayTime.Name" and "r.DayTime"?  What type are these objects?  When you hover over "i" what does it say?
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Friday, July 10, 2009 3:12 PM
    Moderator
  • I do not get any results. If I try to look at the results I get the exception from above. If I REM out the "orderby" clause then everything returns fine.
    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Friday, July 10, 2009 4:46 PM
  • I really need more context here.  For example, some more code would be wonderful. Without having this, there's not a whole lot I can help you with. 

    You might want to try doing something like moving the orderby down:

    var test = from r in data.ReceivingHours
                           where r.EntityType.Name.ToLower().Trim() == "shipto"
                           && r.EntityID == order.CustomerShipTo.ID
                           orderby (int)(DayOfWeek)Enum.Parse(typeof(DayOfWeek), r.DayType.Name) descending
                           select r;

    Or perhaps taking it out and then ordering it in a second call.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Friday, July 10, 2009 4:50 PM
    Moderator
  • Here is an image of the exception.

    http://cid-bc6e963bc5870ac7.skydrive.live.com/self.aspx/Public/LINQ%20Orderby.PNG 


    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Friday, July 10, 2009 4:51 PM
  • Here is an image of the exception.

    http://cid-bc6e963bc5870ac7.skydrive.live.com/self.aspx/Public/LINQ%20Orderby.PNG 


    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”

    Sorry, I can't see that from here.  If I have time tonight, I can take a look at it, but if you want a faster answer, please copy and paste the text here to the forums.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Friday, July 10, 2009 4:56 PM
    Moderator
  • Here is a link to a sample project.

    http://cid-bc6e963bc5870ac7.skydrive.live.com/self.aspx/Public/LINQOrderBy.zip 

    I do not get the exception but I also do not get the days ordered by "DayOfWeek" index.

    Thanks for your help.
    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Friday, July 10, 2009 5:26 PM
  • I'm sorry, I can't tell here.  The code you posted doesn't reproduce the error.  It properly orders the days.  I got the following output:

    Friday
    Thursday
    Monday
    Sunday

    My guess is that your error is from somewhere else in your code.  Try iterating through the whole thing, or calling ".ToList()" on test immediately afterwards.  If the application crashes, post the stack trace and error message here.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by 01000011 Friday, July 10, 2009 6:36 PM
    Friday, July 10, 2009 6:29 PM
    Moderator
  • Your right. I am having issues higher up in my code. Specifically the DayType.Name. I appreciate your help. Sorry you had to work with such little content. I did not want to release company code.

    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Friday, July 10, 2009 6:36 PM