我有这些代码行,它们将项目添加到下拉列表中,并按收听日期和时间对它们进行排序。我想更进一步,按字母顺序按项目的“类型”或描述进行排序。
这是我在控制器中的代码:
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());
}
我的 ..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);
}
}
我试过了:
viewModel.HearingEntryId = hearings.OrderByDescending(h => h.HearingDate).ThenByDescending(d=>d.HearingDate).OrderBy(t=>t.HearingType).FirstOrDefault().HearingEntryId;
确切的错误是:
无法按类型“Kids.Domain.Entities.HearingType”订购。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidOperationException:无法按类型“Kids.Domain.Entities.HearingType”排序。
红色的线是我添加的线.ThenBy(t=>t.HearingType)