score:1

Accepted answer

Can't you just resign from Except and write:

    var rooms = dbt.Rooms.Where(r => r.h_id == AccID)
        .Where(room => !room.Rentals.Any(rental =>
            (dteFrom >= rental.check_in && dteFrom < rental.check_out))).
        .GroupBy(p => p.RoomTypes).Select(g => new RatesViewModel
        {
            TypeName = g.Key.t_name,
            TypeID = g.Key.t_id,
            TypeCount = g.Count()
        })
        .ToList();

score:0

Try using the null coalescing operator: ?? and then getting the IQueryable<T> equivalent of Enumerable<T>.Empty, which is Enumerable<T>.Empty().AsQueryable() and DefaultIfEmpty with an invalid room, so it doesn't go crazy about it being null || empty:

    var rooms = dbt.Rooms.Where(r => r.h_id == AccID)
        .Except(prebookedRooms ?? Enumerable.Empty<T>().AsQueryable().DefualtIfEmpty(new Room() { room_id = -1, hotel_id = -1, type_id = -1}))
        .GroupBy(p => p.RoomTypes).Select(g => new RatesViewModel
        {
            TypeName = g.Key.t_name,
            TypeID = g.Key.t_id,
            TypeCount = g.Count()
        })
        .ToList();

I don't know your type, so I don't know what to put in the Enumerable.Empty<T> call's generic parameter. Add that yourself.

Also, remember to be using System.Linq!


Related Articles