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

  • Home
  • SEARCH
  • 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 6778169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:14:07+00:00 2026-05-26T16:14:07+00:00

Sample in C# and VB.NET are OK. I have a table People with the

  • 0

Sample in C# and VB.NET are OK.

I have a table “People” with the following columns:

-FullName (nvarchar not null)
-DOB (datetime null)

I want to write a LINQ to SQL to group the people by age, like following result:

Age 19: 4 ppl

Age 20: 5 ppl

Age 21: 6 ppl

and so on…

Here’s my try:

Dim query = From ppl In db.People _
         Select New With {.Age = DateTime.Now.Year - CDate(ppl.DOB).Year, .CountAge = ppl.Count}

Notice that there are no DOB record for some people in the tables, so these shall not be included. The DOB column has record like this 1982-10-24 10:12:45 AM because it’s a DateTime column.

  • 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-05-26T16:14:08+00:00Added an answer on May 26, 2026 at 4:14 pm

    I would rather:

    var result = db.People
      .Where(p => p.DOB != null) // exclude null DOB's from stats
      .GroupBy(p => p.DOB.Value.Year)
      .Select(g => new { Age = DateTime.Now.Year - g.Key, Count = g.Count() });
    

    We are grouping against year of birth here. This should translate to GROUP BY YEAR(dob) in SQL which could have slightly better performance or optimization compared to GROUP BY YEAR(GETDATE()) - YEAR(dob). It’s almost impossible to map row expressions into indexes but certain constructs like YEAR() alone could be optimized to use a datetime index partially. I make many assumptions here though, like that datetime structures start with year, SQL server cares about YEAR(x) for optimizations etc. It’s still good to keep in mind such situations when constructing a LINQ query.

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

Sidebar

Related Questions

I have the following sample code in a VB.NET console application. It compiles and
I have a table with the following columns source_title, country, language, source_url I need
Someone have any sample of Comet app .net? I need one sample how to
I have a problem with a sample routing with the preview 5 of asp.net
I have created a sample jsfiddle at http://jsfiddle.net/nair/bv3FG/19/ to demonstrate the problem I am
I have an asp.net gridview which is output as a table in HTML. There's
I am not good with CSS but have downloaded a template off the net
I have a simple ASP.NET table like so: <asp:Table id=tbl> <asp:TableHeaderRow id=header> <asp:TableHeaderCell id=hcell1
i have an asp.net application in c#. i have a table in a sql
I have a table of data and each cell is a link. I want

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.