LINQ query to validate the selected day is not in a list of other dates

Blooming Developer 276 Reputation points
2023-01-31T05:35:57.64+00:00

Hi ,

I want to validate a selected date that does not fall in a list of dates from other table.

Basically when user selects a date from calendar,i need to validate that date is not in list of dates fetched from a table.

How can i acheive this via LINQ query?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,022 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 110K Reputation points
    2023-01-31T06:18:18.3766667+00:00

    Try something like this:

    DateTime selected_date = . . .
    IEnumerable<DateTime> list_of_dates = . . .
    
    bool is_valid = !list_of_dates.Contains( selected_date );
    

    If the time part is present and must be ignored:

    bool is_valid = list_of_dates.All( d => d.Date != selected_date.Date );
    
    0 comments No comments