I have two methods and don’t like the duplication:
public Order LatestOrderOver(decimal amount)
{
return session.QueryOver<Order>()
.Where(o => o.Amount > amount)
.OrderBy(sr => sr.CompleteUtcTime).Desc
.Take(1)
.SingleOrDefault<Order>();
}
public Order LatestAmericanOrderOver(decimal amount)
{
return session.QueryOver<Order>()
.Where(o => o.Amount > amount && o.Country == "USA")
.OrderBy(sr => sr.CompleteUtcTime).Desc
.Take(1)
.SingleOrDefault<Order>();
}
What is the best way to avoid duplication when you have similar criteria (in the Where clause) used in the QueryOver and similar options at the end?
If you are using Linq To Objects, you can just refactor out the delegate:
Otherwise it might work with just changing
Func<>toExpression<>, but I don’t have much experience with that.