When I get a collection of objects from dbContext, is it possible to set an order-by, by default?
For example, say I have a collection of Customers. Inside my controller I can simply do: _context.Customers.Where(.....)
, return a list.
However, I might have multiple calls to _context.Customers.Where(.....)
and I don't want to add Order-by to every instance. Instead I want order-by to happen on retrieving the data automatically.
I'm running .NET Core 2.1.
NO, not possible since it's a explicit operation. Same as in SQL, where you will have to apply order by
clause in order to get ordered data otherwise no order is guaranteed.
So in your case you will have to apply OrderBy()
explicitly saying _context.Customers.Where(.....).OrderBy(...)
. Though you can pull this operation into a method probably to make it reusable