开发者

Syntax error when performing OrderBy<T> on an IEnumerable List

The error message I receive is:

At least one object must implement IComparable

The code causing this is below:

private static IEnumerable<Result> setOrderBy(IEnumerable<Result> value, string order)
{
    if (开发者_Go百科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;
}

A little background: In my MVC2 application, I perform a search in my Search controller. When I send my results to the Results view, I am trying to order the results alphabetically, in ascending or descending order. However, when I write out the logic to set the OrderBy property for my result object I get the squiggly red line underneath the code (as seen in VS2008). For some reason the method doesn't like the data model I am trying to do a sort upon. Each Result object has various properties, one of which is a list of attributes of type string (hence the name StringAttributes) I am trying to sort each Result object in my IEnumerable collection by the value of one of the String Attributes which is present in ALL of my result records.

Help please!


I think you need to use First() or Single instead of Where() in the place where you are picking out the Attribute to order by. At the moment you are asking OrderBy to calculate order using an IEnumerable<Attribute>, rather than a particular attribute.

value = value.OrderBy<Result>(o => o.StringAttributes.Single(p => p.AttributeName == "Title"), new SearchService.ResultComparer<Attribute>());

or

value = value.OrderBy<Result>(o => o.StringAttributes.First(p => p.AttributeName == "Title"), new SearchService.ResultComparer<Attribute>());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜