【发布时间】:2010-06-23 18:11:15
【问题描述】:
我收到的错误信息是:
至少一个对象必须实现 IComparable
导致这种情况的代码如下:
private static IEnumerable<Result> setOrderBy(IEnumerable<Result> value, string order)
{
if (order.Equals("ASC"))
{
//value = value.OrderBy(c => c, new SearchService.ResultComparer<Attribute>());
value = value.OrderBy<Result>(o => o.StringAttributes.Where(p => p.AttributeName == "Title"), new SearchService.ResultComparer<Attribute>());
//value = value.OrderBy(o => o.StringAttributes.Where(p => p.AttributeName == "Title"), new SearchService.ResultComparer<AttributeItem>()));
}
if (order.Equals("DESC"))
{
value = value.OrderByDescending(c => c, new SearchService.ResultComparer<Attribute>());
//value = value.OrderByDescending(o => o.StringAttributes.Where(p => p.AttributeName == "MatterName"));
}
return value;
}
一点背景: 在我的 MVC2 应用程序中,我在我的搜索控制器中执行搜索。当我将结果发送到结果视图时,我试图按字母顺序、升序或降序对结果进行排序。 然而,当我写出为我的结果对象设置 OrderBy 属性的逻辑时,我在代码下方看到一条弯曲的红线(如 VS2008 中所示)。由于某种原因,该方法不喜欢我尝试对其进行排序的数据模型。每个 Result 对象都有各种属性,其中一个是字符串类型的属性列表(因此命名为 StringAttributes)我的结果记录。
请帮忙!
【问题讨论】:
-
你在哪一行得到错误?
-
顺便说一句,最好使用
if() {} else if() { },因为只有一个可能 -
谢谢。通常当我编程时,我先让它工作,然后重构。一定喜欢敏捷编程吧?
标签: c# linq asp.net-mvc-2 ienumerable sql-order-by