Edit/Note from Bob Horn: This answer was accepted because of the EDIT below, specifying a bug in EF. The first part of this answer didn't work.
By using object.Equals(object, object)
as method of comparing two value types (Nullables are value types too), you are implicitly boxing them. Entity Framework does not support this.
Try using the ==
operator instead:
// Since scan.RawMaterialLabel.ManufacturerId and manufacturerId are both Nullable<T> of the
// same type the '==' operator should assert value equality, whether they have a value, or not.
// (int?)1 == (int?)1
// (int?)null == (int?)null
// (int?)1 != (int?)null
predicate = predicate.And(scan => scan.RawMaterialLabel.ManufacturerId == manufacturerId);
For value types the ==
operator asserts value equality, similarly to what the object.Equals() does for reference types.
EDIT:
Upon further investigation, there seems to be a bug in older versions of EF (pre EF5).
Hacky fix with your current version:
predicate = predicate.And(scan =>
manufacturerId.HasValue
? scan.RawMaterialLabel.ManufacturerId == manufacturerId
: scan.RawMaterialLabel.ManufacturerId == null);
But if your application allows for it, upgrade to EF5.