Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7954945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:35:08+00:00 2026-06-04T03:35:08+00:00

Using LINQ to Entities, I need to get records from a PersonTable WHERE Age

  • 0

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.

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);

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.”

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:35:10+00:00Added an answer on June 4, 2026 at 3:35 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using LINQ to Entities, how can I determine if any item from a List
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
Due to the potential differences between Linq-to-Entities (EF4) and Linq-to-Objects, I need to use
I am using LINQ-to-Entities and need to populate a ListView using a select statement
The below report is currently written using linq to entities to get the data
I'm using LINQ to Entities to display paged results. But I'm having issues with
I'm using LINQ to Entities and I want to know how do I translate
I'd like to implement MVC while using LINQ (specifically, LINQ-to-entities). The way I would
Is it worth using System.Transactions.TransactionScope on Linq to Entities? On the MS documentation ,
I'm working on the following LINQ query: public void GetAuditRuleAgencyRecords(IEnumerable<Entities.AuditRule> rules) { using (LinqModelDataContext

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.