I am trying to fetch a EF model executing the following query:
SELECT *
FROM Vehicles
WHERE (TypeId = 1 AND PlanId = 1) OR (TypeId = 2 AND PlanId = 2) OR (TypeId = 3 AND PlanId = 3);
In order to do so, I have the following method:
public IEnumerable<Vehicle> GetByIds()
{
IEnumerable<VehicleId> resultIds = new List<VehicleId>()
{
new VehicleId(1, 1),
new VehicleId(2, 2),
new VehicleId(3, 3)
};
var vehicleList = DbSet
.Where(resultIds.Contains(new VehicleId(s.TypeId, s.PlanId)))
.ToList();
return vehicleList;
}
// ...
public class VehicleId
{
public readonly int TypeId;
public readonly int PlanId;
public VehicleId(int type, int plan)
{
TypeId = type;
PlanId = plan;
}
}
However, this is giving me the error:
System.NotSupportedException: Unable to create a constant value of type 'VehicleRepository+VehicleId'. Only primitive types or enumeration types are supported in this context.
I already checked older questions and:
- performing
.Select().Where()
won't fit since I have a huge amount of unfiltered data .Where(s => resultIds.Any(r => r.TypeId == s.TypeId && r.PlanId == s.PlanId))
throws same error