Ok, I have a simple IEnumerable<HtmlString> things and I want to divide it up into four equal groups.
var quarter = things.OrderBy(t => t.Foo).Count() / 4;
should do the trick, but instead I get this funkiness:
Server Error in ‘/’ Application. At least one object must implement
IComparable. 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.ArgumentException: At least one object must
implement IComparable.Line 36: int quarter = things.OrderBy(t => t.Foo).Count() / 4;
Anyone know what the heck is going on here? Why would I need to implement IComparable to get a simple count?
My guess is that this is to do with lazy evaluation of LINQ’s
OrderBy. For example, if you have:then if the
foo.Barproperties can’t be compared with each other, that will throw exactly that exception.For example:
Output: