0

I have these lines of code which adds item to a dropdown list and sorts them by hearing date and then by time. I would like to take it a step further and sort by the 'type' or description of the item in alphabetical order.

this is my code in my Controller:

public void AddHearingsToViewModel(CourtActivityViewModel viewModel, IQueryable<Hearing> hearings)
    {
        if (viewModel.HearingEntryId == Guid.Empty)
            viewModel.HearingEntryId = hearings.OrderByDescending(h => h.HearingDate).ThenByDescending(d=>d.HearingDate).FirstOrDefault().HearingEntryId;
        viewModel.Hearings = hearings.ToSelectList("HearingEntryId", "CourtActivitySelection", viewModel.HearingEntryId.ToString());
    }

My ..Domain.Entities.Hearing

public partial class Hearing
{

    public string CourtActivitySelection
    {
        get { return string.Format(@"{0:d} - {0:t} - {1} ", HearingDate, HearingType.Description); }
    }

    public override string ToString()
    {
        return string.Format(@"{1} on {0:d} @ {0:t}", HearingDate, HearingType.Description);
    }
}

public partial class HearingEntry
{

    public override string ToString()
    {
        return string.Format(@"{1} on {0:d} @ {0:t}", HearingDate, HearingType.Description);
    }

}

I tried:

viewModel.HearingEntryId = hearings.OrderByDescending(h => h.HearingDate).ThenByDescending(d=>d.HearingDate).OrderBy(t=>t.HearingType).FirstOrDefault().HearingEntryId;

The exact error is :

Cannot order by type 'Kids.Domain.Entities.HearingType'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Cannot order by type 'Kids.Domain.Entities.HearingType'.

And the line in red is the line i added the .ThenBy(t=>t.HearingType)

5
  • Your .OrderBy(t=>t.HearingType) should probably be .ThenBy(t=>t.HearingType). Commented Jun 1, 2012 at 13:54
  • @ErikPhilips just tried it and i still get the same error. I am not sure what that error really means like what would be the reason for that. Commented Jun 1, 2012 at 13:56
  • It would be best if you could copy and paste the exact error message, as we have no context for what your model looks like, nor what each property type is. Commented Jun 1, 2012 at 13:57
  • Did you implement IComparable in your objects? Commented Jun 1, 2012 at 14:02
  • @ErikPhilips added more code and error message Commented Jun 1, 2012 at 14:11

1 Answer 1

1

Looks like your HearingType is a class that does not implement IComparable. Either implement this interface on the Type that HearingType is

public SomeClassThatHearingTypeIsAnInstanceOf : IComparable
{
}

or change your code to

.ThenBy(t=>t.HearingType.SomePropertyYouWantToSortBy)

The only problem you may run into (since I don't know how your classes are populated, is that HearingType could be null.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help it worked for me! and about the hearing type could be null i already check for that so i will be good to go. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.