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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:41:54+00:00 2026-05-24T04:41:54+00:00

For example, I have some data source (probably, database) from where I need to

  • 0

For example, I have some data source (probably, database) from where I need to extract all records for the last month. I’m using LINQ. There are two possible ways how to achieve this:

First:

var res = from rec in _records
          where rec.RequestDateTime.Date.Year == date.Year &&
                rec.RequestDateTime.Date.Month == date.Month
          select rec;

Second (using direct Date comparison):

var year = date.Year;
var month = date.Month;

var monthStart = new DateTime(year, month, 1);
var monthEnd = new DateTime(year, month, DateTime.DaysInMonth(year, month));

var res = from rec in _records
          where rec.RequestDateTime.Date >= monthStart &&
                rec.RequestDateTime.Date <= monthEnd
          select rec;

I heard that the second code works faster for the most LINQ providers, but can’t find any proofs or explanations (or disproofs). So, what way actually better to use for better performance and why?

  • 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-24T04:41:54+00:00Added an answer on May 24, 2026 at 4:41 am

    I’d go further – IMO it will work faster for almost all providers (not least, LINQ-to-Objects). When talking about indexed data – database indexes are usually sorted, so taking a selection based on a range is really really quick and easy. There is no interpretation required by >= and <=, and it can jump directly to the required range very quickly. However, if you are comparing the year and month, it has two choices:

    • calculate the year / month (based on the numeric value underpinning a date) and compare for equality – and note it may or may not be able to use the index, needing instead a full scan
    • do some very clever thinking to determine that it is a range (making query parse more complex)

    For the simplest case (LINQ-to-Objects) the same holds true; >= etc is trivial – it is a numeric compare. Testing month/year requires computing the month / year. That data is not stored explicitly as integers and is not directly on hand. Yes it isn’t overly expensive to calculate, but when considering date en-masse, it isn’t free either.

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

Sidebar

Related Questions

I'm using Hibernate to retrieve some data from a database. The result set returned
I have some data with messed-up accented characters. For example in the data we
I have some .net apps running that I need to monitor for example, then
I have to pass some parameter from an action to another action,for example to
I have to request data for a JS-script from a MySQL database (based upon
php newbie here..I need some PHP help ideas/examples on how to import data from
I have a javascript array that I am using as a data source in
What I need to do is some data between 2 databases. The source can
I have an NSMutableDictionary with some song data (for example key: Artist object: Passion
Has anyone done this / have some example code?

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.