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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:25:20+00:00 2026-06-07T05:25:20+00:00

This is a follow up question to the one answered here: Excluding dates from

  • 0

This is a follow up question to the one answered here: Excluding dates from Linq Query in MVC .net application – which I’m very grateful for.

I’m hoping that someone can check my syntax in my Linq query below – to confirm if it’s the best way to build the query up, or if my use of the syntax is inefficient.

public class Room 
{ 
    public int RoomId { get; set; } 
    [Display(Name = "Room Name")] 
    public string Name { get; set; } 
    public bool Disabled { get; set; } 
    public virtual ICollection<Client> Clients { get; set; } 
    } 

public class Client 
{ 
    public int ClientId { get; set; } 
    public int RoomId { get; set; } 
    public string ClientName { get; set; } 
    public DateTime Arrival { get; set; } 
    public DateTime Departure { get; set; } 
    public virtual Room Room { get; set; } 
} 

Clients lists a row for each client who has a particuar room booked. I have 3 rooms, Room 1, Room 2, and Room 3. So entries in the client table could be:

Client 1, Room 1, Mr Smith, Arr: 2012-07-08, Dep: 2012-07-10
Client 2, Room 1, Mr Jones, Arr: 2012-07-14, Dep: 2012-07-20
Client 3, Room 2, Mr Alas,  Arr: 2012-07-12, Dep: 2012-07-15

Given an arrival and departure date, I’m trying to take my whole list of rooms, and take away any that have a client staying where the arrival or departure dates overlap. So using the data above, if I had an arrival date of 2012-07-12 and a departure date of 2012-07-13, then Room 2 would not be available, however, Room 1, does not have any bookings spanning that date – so Room 1 I want to leave in my result set.

So my Linq query (I’m new to Linq, so please point out where I may be going wrong) is:

var dteFrom = DateTime.Parse("2012-07-12");
var dteTo = DateTime.Parse("2012-07-13");
var rooms = (from r in Rooms
                where !r.Clients.Any(
            client =>
                ( dteFrom >= client.Arrival && dteFrom <= client.Departure )
                ||
                ( dteTo >= client.Arrival && dteFrom <= client.Departure )
                ||
                ( dteFrom <= client.Arrival && dteTo >= client.Departure )
                )
            select r);

Given that I’m looking to include ALL rooms, EXCEPT any that meet the criteria, can anyone confirm that my use of .Any and ! and || are correct, as far as LINQ goes?

Is there any better way within the syntax, of excluding records from the Rooms list?

Thank you again for any help,

Mark

  • 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-06-07T05:25:21+00:00Added an answer on June 7, 2026 at 5:25 am

    Looks fine to me – one thing that may help readability would be to compose your query in two steps:

    var prebookedRooms = rooms
        .Where(room => room.Clients.Any(client => 
        (dteFrom >= client.Arrival && dteFrom <= client.Departure) ||
        (dteTo >= client.Arrival && dteFrom <= client.Departure)   ||
        (dteFrom <= client.Arrival && dteTo >= client.Departure)));
    
    var freeRooms = rooms.Except(prebookedRooms);
    

    Remembering that the query is only executed, when the results are enumerated – so there’s no performance cost to doing this in two steps.

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

Sidebar

Related Questions

This is a follow-on question from the one I asked here . Can constraints
This is a follow up question from my previous one found here I need
This question is a follow-up to one I asked and got answered here: How
Just a follow up question to this one here => link Is it possible
This question is a follow on from this one ... I am binding to
Note that this is a follow-up question from my previous one: How to memorize
this is a follow-up question to my last one here: iOS: Initialise object at
PHP mysql database I have created a follow on question to this one here
This is a follow-up question to the one I posted here (thanks to mario
This is a follow up question to this one: Query examples in a many-to-many

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.