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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:24:21+00:00 2026-05-26T22:24:21+00:00

I have a DataTable which has the following structure. StartDate (Type DateTime) EndDate (Type

  • 0

I have a DataTable which has the following structure.
StartDate (Type DateTime)
EndDate (Type DateTime)
Description (Type Text)

What I need to check is if the dates overlap or not, i.e., if the Start Date of 1 item is less than the EndDate of the previous item.

Here is the code I wrote.

NB: I already have checks to ensure that for each record, the Start Date has to be lesser than the End Date

dtDates.DefaultView.Sort = "StartDate ASC"; //Sort the DataTable based on the Start Dates
if(dtDates.Rows.Count > 1) //Check only if more than 1 Date is specified
{
    for (int i = 1; i < dtDates.Rows.Count; i++)
    {
        if(TypeConvert.ToDate(dtDates.Rows[i]["StartDate"] < TypeConvert.ToDate(dtDates.Rows[i-1]["EndDate"])
            {
                retVal = true;
                currentRow = i;
                break;
            }
     }
}

The above code works fine.

But now, I want to implement it using LINQ

So, here is the code I am trying to attempt.

How can I do the whole comparison using LINQ?

    public static bool CheckIfDatesOverlap(DataTable dataTable)
    {
        bool retVal = false;
        int currentRow = 0;
        List<DataRow> listDates = (from DataRow dgv in dataTable.Rows
                                               select dgv).OrderBy(x => x[StartDate]).ToList();
        if (listDates.Count > 1) //Perform Comparision only if more than 1 row is present
        {
            for (int i = 1; i < listDates.Count; i++)
            {
                if (TypeConvert.ToDateTime(listDates[i][StartDate]) < TypeConvert.ToDateTime(listDates[i][EndDate]))
                {
                    retVal = true; //There are duplicates, hence return true
                    currentRow = i;
                    break;
                }
                else
                {
                    //Do nothing as dates do not overlap
                }
            }
        }
        else
        {
            retVal = false; //As there is only 1 row, return false
        }
        if (retVal)
        {
            string message = "Dates Overlap";
            //Display message
        }
        return retVal;
    }
  • 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-26T22:24:22+00:00Added an answer on May 26, 2026 at 10:24 pm

    If you use SelectWithPrevious described here then you can write the following:

    bool datesOverlap = table.Rows
        .Cast<DataRow>()
        .SelectWithPrevious((current, previous) => new
            {
                PreviousEndDate = (DateTime)previous["EndDate"],
                StartDate = (DateTime)current["StartDate"]
            })
        .Any(x => x.StartDate < x.PreviousEndDate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following datatable, which has Units information. The Units have a surface
I have a DataTable which is bound to datagridview (Winforms)... I use the following
I have the DataTable with following columns: id, Name, Description, ParentId and would like
I have a dataTable which has a value of a bean class which looks
I have one DataTable which has four columns such as MileStoneID MileStoneName Percentage SeqNbr
I've got a DataTable which has a number of records. I've tried the following
Which of the following has the best performance? I have seen method two implemented
I have a DataTable which has 5 columns: ID Name Account Number Branch Amount
I have a DataTable which is bound to DataGridView . DataTable has several columns
I have two DataTables which has the following rows. Table A: TaxID TaxName ------

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.