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 8402225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:00:09+00:00 2026-06-09T22:00:09+00:00

How do I get persons whose age is over 21 using EF? from person

  • 0

How do I get persons whose age is over 21 using EF?

from person in context.Persons
where DateTime.UtcNow - person.Dob > TimeSpan.FromHours(184086)
select new {person, Age = DateTime.UtcNow - person.Dob}

The above failed.

What can I do to fix this? Is the above supported in the more recent version of EF eg. 4.2+

Can I access the properties of the DateTime struct as follow?

from person in context.Persons
where DateTime.UtcNow.Year - person.Dob.Year > 21
select new {person, Age = DateTime.UtcNow.Year - person.Dob.Year}
  • 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-09T22:00:11+00:00Added an answer on June 9, 2026 at 10:00 pm

    You can use the < operator in linq-to-entites queries. You can’t use addition or subtraction though. Fortunately, SqlFunctions has functions to calculate date differences. So what you can do is this:

    var d21 = DateTime.Today.AddYears(-21);
    var q = from person in context.Persons
            where person.Dob < d21
            select new
            {
                person,
                Age = SqlFunctions.DateDiff("year", person.Dob, DateTime.Today)
            }
    

    You have to create the variable d21 first, otherwise EF complains about not knowing AddYears.

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

Sidebar

Related Questions

I want to get phone no of the person from his name which are
I have an simple class that I get from a webservice. public class person
I've a php function to get some data from mysql database: function get_persons() {
In the following code, i have a method to get a Vector of persons
Please consider the following code: public class Person ( public string FirstName {get; set;}
I'm not a UI person, so I'm hoping to get some suggestions for how
I want to get an object by a static factory method, such as Person
public class Person{ @OneToMany(orphanRemoval = true) List<Cars> myCars; //Get and Set } public class
Suppose I have table Person table Employee, which inherits Person. I want to get
Given this object class Contact { public IEnumerable<Person> People { get; } public string

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.