By using the following
PagedData.Products = from p in db.Products
where (from m in p.Manufacturers
where model.man.Contains(m.ManufacturerID)
select m).Any()
where (from s in p.Sizes
where model.size.Contains(s.SizeID)
select s).Any()
where (from c in p.Colors
where model.color.Contains(c.ColorID)
select c).Any()
select p;
i get this error
Unable to create a null constant value of type 'System.Int32[]'. Only entity types, enumeration types or primitive types are supported in this context.
I got the point of the error, but i cannot figure out how should i fix it.
The model.man
model.size
and model.color
are arrays of integer, that may be also null.
Since all conditions must be true to pass any Product
you should first check if all arrays have any content at all:
if (model.man != null && model.size != null && model.color != null
&& model.man.Any() && model.size.Any() && model.color.Any())
{
PagedData.Products = from p in db.Products ...
Now you won't execute a query if you know upfront that it doesn't return any data anyway. And it will not throw the exception because you never run the query with null
arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With