I it was possible to do this in Active Record, but is this feasible with Linq to SQL?
A:
You can if you use stored procedures or views to retrieve your data. The ORDER BY can then be in the stored proc or view but LINQ to SQL doesn't support this out of the box.
Ray Booysen
2009-02-08 21:11:01
+1
A:
You can always add a property to the data-context / entities objects in a partial class file:
partial class MyDataContext {
public IOrderedQueryable<Foo> FoosByName {
get {return Foos.OrderBy(foo=>foo.Name);}
}
}
Then any queries started from FoosByName
will be pre-ordered (but still composable).
Marc Gravell
2009-02-08 21:16:47
This works great. I've taken it a little farther by adding an interface "IHasOrder" then writing an Extension on the IQueryable<IHasOrder> to have a Method called DefaultOrder which uses the order from the interface.
Kelly
2009-02-08 21:34:56