Assume I have an entity in my (Code First) Entity Framework model that looks like this:
public class Foo
{
[Key]
[MaxLength(50)]
public string FooId { get; set; }
[Timestamp]
[ConcurrencyCheck]
public byte[] Version { get; set; }
}
Assume further that I need to fetch the most recent few of these items. I can do that with a fairly simple LINQ expression, which gets translated into the correct SQL, and runs fine.
var recentFoos = db.Foos.OrderBy(f => f.Version).Take(10);
Now, if that query happens in a method that I also want to test, things get more complicated. I have a setup with fake context and entity collections, and that all works fine. But, because byte[] can't be sorted directly, trying to execute that code throws an ArgumentException ("At least one object must implement IComparable.").
On the other hand, if I use the overload of OrderBy() that take a comparer, that will run in memory, but throw an exception ("…this method cannot be translated into a store expression.") trying to run against the database.
Is there a way to either a) write a single LINQ query that will run correctly in both circumstances, or b) to reliably detect which case pertains, so that my code can sort using the correct overload?