I'm having a problem using a UOW pattern and Entity Framework. The first linq below, orders1, works fine and generates a single SQL statement for SQL Server - it's a straight out-of-the-box database-first generated EF context. In the second LINQ below, orders2, I'm trying to get ready for a UOW pattern and wrapped the database in a Repository object - however it throws the exception: "Unable to create a constant value of type yyy. Only primitive types or enumeration types are supported in this context."
The point of the linq is to find orders where the same RiskID has not been ordered in the previous 30 days. Is there someway to rewrite the linq so that orders1 would not invoke the context twice; or is there some technique I can follow in the repository pattern so i can refer to the same table again in a subquery?
All of this is EF5.
using (var db = new xxxEntities())
{
var orders1 =
(from order in db.Orders
where !db.Orders.Any(otherOrder =>
otherOrder.RiskId == order.RiskId &&
otherOrder.DateTimeOrdered < order.DateTimeOrdered &&
otherOrder.DateTimeOrdered > EntityFunctions.AddDays(order.DateTimeOrdered, -30))
select order
).ToList();
}
var repository = new Repository<Order>(new xxxEntities());
var orders2 =
(from order in repository.DbSet
where !repository.DbSet.Any(otherOrder =>
otherOrder.RiskId == order.RiskId &&
otherOrder.DateTimeOrdered < order.DateTimeOrdered &&
otherOrder.DateTimeOrdered > EntityFunctions.AddDays(order.DateTimeOrdered, -30))
select order
).ToList();