I created an extension method to encapsulate some logic where (this is a very simplified version):
public static IQueryable<Cargo> ReadyToCarry(this IQueryable<Cargo> q)
{
VehicleType[] dontNeedCouple = new VehicleType[] { VehicleType.Sprinter, VehicleType.Van, VehicleType.Truck };
return q.Where(c => c.DriverId > 0 && c.VehicleId > 0)
.Where(c => c.CoupleId > 0 || dontNeedCouple.Contains(c.Vehicle.Type));
}
Therefore, I can use it as follows:
using (var ctx = new MyNiceContext())
{
var readyCargo = ctx.Cargos.ReadyToCarry().OrderBy(c => c.Id).ToList();
}
What works well is translated into SQL and implemented by the Entity Framework. Now I have another place where I need loads that are not ready to carry, which means that I need something completely different.
My idea was something like this:
public static IQueryable<Cargo> NotReadyToCarry(this IQueryable<Cargo> q)
{
return !q.ReadyToCarry();
}
using (var ctx = new MyNiceContext())
{
var readyCargo = ctx.Cargos.NotReadyToCarry().OrderBy(c => c.Id).ToList();
var readyCargo = ctx.Cargos.ReadyToCarry(false).OrderBy(c => c.Id).ToList();
}
I did not want to recreate the inverse logic from scratch, so if I had to change it once, I would change one unique place.
I accept alternatives to this approach, as this is a new project.