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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:19:44+00:00 2026-05-30T10:19:44+00:00

I have DateStart , DateEnd Periodicity , TypePeriodicity fields. We have a query: var

  • 0

I have DateStart, DateEnd Periodicity, TypePeriodicity fields.

We have a query:

var result = Events.Where(e => e.DateStart <=today && e.DateEnd >= today).ToList();

I want that this query to check Periodicity.

For example:

name  - record1
DateStart = 2012-02-02
DateEnd = 2012-03-31
Periodicity = 2
TypePeriodicity = 1 ( it's mean a week, may be also day = 0, month=2): 

I want the following, if current date equals:

2,3,4,5 February - return `record1`   
6,7,8..12 - not return, because TypePeriodicity = 1 and Periodicity = 2, which means every 2 weeks
13..19 - return `record1`
20..26 - not return  
and so on until `DateEnd`

Thanks.

PS. Maybe not LINQ, but simple method that recieve result as parameter.

  • 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-30T10:19:46+00:00Added an answer on May 30, 2026 at 10:19 am

    Here is something to get you started:

    You could define a DateEvaluator delegate like so:

        delegate bool DateEvaluator(DateTime startDate, DateTime endDate, DateTime dateToCheck, int periodicity);
    

    The purpose of the delegate would be to evaluate for a given periodicity type if a date should be considered as within range. We would have hence 3 date evaluators.
    One for each period type: Lets call them dayPeriodicityChecker, weekPeriodicityChecker and monthPeriodicityChecker

    Our dayPeriodicityChecker is straightforward:

            DateEvaluator dayPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
                {
                    if ((dateToCheck < startDate) || (dateToCheck > endDate))
                        return false;
    
                    TimeSpan dateDiff = dateToCheck - startDate;
                    return dateDiff.Days % periodicity == 0;
                };
    

    Our weekPeriodicityChecker needs to account for the start day of week, so the start date would need to be adjusted to the date in which the startDate week actually starts:

            DateEvaluator weekPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
                {
                    if ((dateToCheck < startDate) || (dateToCheck > endDate))
                        return false;
    
                    DateTime adjustedStartDate = startDate.AddDays(-(int)startDate.DayOfWeek + 1);
                    TimeSpan dateDiff = dateToCheck - adjustedStartDate;
                    return (dateDiff.Days / 7) % periodicity == 0;
                };
    

    Our monthPeriodicityChecker needs to cater for months with a variable number of days:

            DateEvaluator monthPeriodicityChecker dateToCheck, periodicity) =>
                {
                    if ((dateToCheck < startDate) || (dateToCheck > endDate))
                        return false;
    
                    int monthDiff = 0; 
                    while (startDate.AddMonths(1) < dateToCheck)
                    {
                        monthDiff++
                        // i'm sure there is a speedier way to calculate the month difference, but this should do for the purpose of this example 
                    }
    
                    return (monthDiff - 1) % periodicity == 0;
                };
    

    Once you have all your date evaluators defined you could put them in an array like so:

            DateEvaluator[] dateEvaluators = new DateEvaluator[] 
            {
                dayPeriodicityChecker,
                weekPeriodicityChecker,
                monthPeriodicityChecker
            };
    

    This will allow you to do :

    int periodicityType = 0; // or 1=week or 2=months
    bool isDateIn = dateEvaluators[periodicityType ](startDate, endDate, dateTocheck, Periodicity)
    

    So lets test this:

            PeriodicityEvent pEvent = new PeriodicityEvent
            {
                Name = "record1",
                DateStart = new DateTime(2012, 02, 02),
                DateEnd = new DateTime(2012, 03, 31),
                PeriodicityType = 1,
                Periodicity = 2
            };
    
            DateTime baseDate = new DateTime(2012, 02, 01);
            for (int i = 0; i < 30; i++)
        {
                DateTime testDate = baseDate.AddDays(i);
                if (dateEvaluators[pEvent.PeriodicityType](pEvent.DateStart, pEvent.DateEnd, testDate, pEvent.Periodicity))
                {
                    Console.WriteLine("{0} is in", testDate.ToString("dd MMM"));
                }
                else
                {
                    Console.WriteLine("{0} is out", testDate.ToString("dd MMM"));
                }
       }
    

    This will produce the desired output as below:

    Week evaluation output

    To use you would simply do:

    Events.Where(e => dateEvaluators[e.PeriodType](e.DateStart, e.DateEnd, today, e.Periodicity).ToList();
    

    Good luck!

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

Sidebar

Related Questions

I have a table with 3 fields: id datestart dateend I need to query
I have the following code. dateStart & dateEnd are both DateTime. My issue is
I have a data result that I want to filter by two different dates.
I have this command that I run every 24 hours currently. find /var/www/html/audio -daystart
I have a file of the following format Summary:meeting Description:None DateStart:20100629T110000 DateEnd:20100629T120000 Time:20100805T084547Z Summary:meeting
For example, we have event that will go every 3 month. Event have DateStart,
I have a form that I want to submit to a url. I am
I would like to have a simple query that I can run against a
I have this command that I run everyday via cron: find /home/get/public_html/videos -daystart -maxdepth
my question is: assuming we have a calendar_table with UNIX datestamp date_column, 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.