I have a LINQ query which I'm using to select some rows out of a SQL database. Each row from my database represents a footprint of physical space, and I'm trying to select just the rows which contain a given point. The part of the query that is giving me issues is below:
LinqMetaData meta = new LinqMetaData(da);
var captures = (from c in meta.Capture
where
(c.TLLat.HasValue && lat < Math.Max(Math.Max(c.BLLat.Value, c.BRLat.Value), Math.Max(c.TLLat.Value, c.TRLat.Value)))
&&
(c.TLLat.HasValue && lat > Math.Min(Math.Min(c.BLLat.Value, c.BRLat.Value), Math.Min(c.TLLat.Value, c.TRLat.Value)))
select c);
When I run the code, I get this error:
The binary expression '(39.3237282094724 < Max(Max(EntityField(LPLA_1.BLLat AS BLLat), EntityField(LPLA_1.BRLat AS BRLat)), Max(EntityField(LPLA_1.TLLat AS TLLat), EntityField(LPLA_1.TRLat AS TRLat))))' can't be converted to a predicate expression.
I've assumed this means I can't use Math.Max() / Math.Min() in a LINQ query...Is this correct?
EDIT:
I am using LinqMetaData for my query, which comes from the LLBLGen Pro c# library. I think that this implementation of LINQ may not support Math.Max() / Math.Min() within its queries.
Simply move
Max
andMin
calculations outside of your query as separate variables.Not only that could affect your query overall performance but will remove the error you are seeing which is in this case connected to the provider unable to translate nested
Max
andMin
methods.Another approach would be to convert the above query to pure SQL Procedure.