简体   繁体   English

我可以在 LINQ 查询中使用 Math.Max() / Math.Min() 吗?

[英]Can I use Math.Max() / Math.Min() within a LINQ query?

I have a LINQ query which I'm using to select some rows out of a SQL database.我有一个 LINQ 查询,我使用它来 select 数据库中的一些行。 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.我的数据库中的每一行都代表一个物理空间的足迹,我试图 select 只是包含给定点的行。 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.二进制表达式 '(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)) ))' 不能转换为谓词表达式。

I've assumed this means I can't use Math.Max() / Math.Min() in a LINQ query...Is this correct?我假设这意味着我不能在 LINQ 查询中使用 Math.Max() / Math.Min() ...这是正确的吗?

EDIT:编辑:

I am using LinqMetaData for my query, which comes from the LLBLGen Pro c# library.我正在使用 LinqMetaData 进行查询,它来自 LLBLGen Pro c# 库。 I think that this implementation of LINQ may not support Math.Max() / Math.Min() within its queries.我认为 LINQ 的这种实现可能在其查询中不支持 Math.Max() / Math.Min() 。

Simply move Max and Min calculations outside of your query as separate variables.只需将MaxMin计算作为单独的变量移到查询之外。
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 and Min methods.这不仅会影响您的查询整体性能,而且会消除您看到的错误,在这种情况下,连接到提供程序无法翻译嵌套的MaxMin方法。

Here is a Microsoft Documentation about some of the known issues with LINQ to Entities. 是有关 LINQ 到实体的一些已知问题的 Microsoft 文档。

Another approach would be to convert the above query to pure SQL Procedure .另一种方法是将上述查询转换为纯SQL Procedure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM