LINQ到实体:年龄计算在LINQ查询导致"方法不能被翻译成店前pression"翻译成、实体、年龄、方法

2023-09-03 22:08:20 作者:无名指的约定

使用LINQ到实体,我需要从代码PersonTable WHERE年龄是AgesFrom和AgesTo之间得到记录。现在的问题是,我需要计算从一个DATEOFBIRTH和特定DateToCalculate,两者存储在数据库中的年龄

Using LINQ to Entities, I need to get records from a PersonTable WHERE Age is between AgesFrom and AgesTo. The problem is, I need to calculate their Age from a DateOfBirth and a specific DateToCalculate, both stored in the database.

如。 代码PersonTable:DATEOFBIRTH = 01/04/1972年,DateToCalculateAge = 25 /二千○十一分之一十二

eg. PersonTable: DateOfBirth=01/04/1972, DateToCalculateAge=25/12/2011

.Where(0 < o.FormDate.AddYears(-agesFrom).CompareTo(o.FormDate.Subtract(o.Person.DateOfBirth)) &&
       0 > o.FormDate.AddYears(-agesTo).CompareTo(o.FormDate.Subtract(o.Person.DateOfBirth));
// ----------------------- OR ------------------------
.Where(agesFrom <= CalculateAge(o.Person.DateOfBirth.Value, o.FormDate) &&
       agesTo >= CalculateAge(o.Person.DateOfBirth.Value, o.FormDate);

有关我提供code,我得到以下异常:LINQ到实体不能识别方法的System.DateTime AddYears(Int32)在​​法,这种方法不能被翻译成店前pression。

For my provided code, I get the following Exception: "LINQ to Entities does not recognize the method 'System.DateTime AddYears(Int32)' method, and this method cannot be translated into a store expression."

我明白,一旦通过LINQ查询过的分贝,方法(S)我试图用无法翻译。是否有变通方法吗?我可以过滤进入数据库后,但要避免那种瓶颈。我也想过将SQL函数来执行此计算为我,而是想先用尽LINQ的可能性。

I understand that once LINQ passes the Query over to the db, the method(s) I were trying to use could not be translated. Is there a work around for this? I could filter after hitting the database, but want to avoid that sort of bottle neck. I also thought of adding a sql function to do this calculation for me, but want to exhaust the LINQ possibilities first.

一个很好的张贴在这里详细介绍它是如何更好的查询之前要做的年龄计算: Shortest的方式来为您在LINQ年龄过滤器?的然而,在我的情况,我不是从DateTime.Now从数据库的日期计算时代,而是时代。

A nice post here details how it is better to do the age calculation before the query: Shortest way to check for age filter in LINQ? However in my situation, I am not calculating age from DateTime.Now, but rather age from a date in the db.

推荐答案

由于您使用LINQ到实体,在preferred解决方案是利用中的 System.Data.Objects.EntityFunctions ( 为EF 6版本更新:使用的 DbFunctions 而不是的),查询表面会成功地把它们翻译成SQL。在这种情况下,你要采取如这样的:

Since you are using LINQ to Entities, the preferred solution is to utilize the methods defined in System.Data.Objects.EntityFunctions (update for EF version 6: use DbFunctions instead) and the query surface will translate them into SQL successfully. In this case you want to take e.g. this:

o.FormDate.AddYears(-agesFrom)
          .CompareTo(o.FormDate.Subtract(o.Person.DateOfBirth))

,并将其转换为类似

and convert it to something like

EntityFunctions.DiffMinutes(
    EntityFunctions.AddYears(o.FormDate, -agesFrom),
    EntityFunctions.AddSeconds(
        o.FormDate, 
        EntityFunctions.DiffSeconds(o.FormDate, o.Person.DateOfBirth)
    )
)

我也不太清楚,如果上面的其实是正确的,因为它的立场,但因为它确实伤害了我的眼睛。

I 'm not sure if the above is actually correct as it stands though because it really hurts my eyes.