views:

195

answers:

2

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
+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
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