Domanda

I am writing a linq having a left outer join with conditions on right side table. my code

 var leftHotelRooms = db.HotelRooms.Include(hr=>hr.HotelRoomBlackoutPeriods)
                    .Where(room => hotelCodes.Contains(room.UserHotelId) &&
                      room.Scope == scope &&
                      room.IsDeleted == false &&
                      room.IsEnabled == true &&
                      room.MaxCapacity >= minimumNumberOfGuests)
                    .GroupJoin(db.HotelRoomBlackoutPeriods,
                              room => room.Id,
                              blackoutPeriod => blackoutPeriod.HotelRoomId,
                              (room, blackoutPeriod) => new
                              {
                                 room,
                                 blackoutPeriods = blackoutPeriod.DefaultIfEmpty()
                               })
                    .Select(a => new {a.room,a.blackoutPeriods})
                    .Where(
                           x => x.blackoutPeriods.Any(y => DbFunctions.DiffDays(y.StartDate, checkIn) != 0
                    &&    (DbFunctions.DiffDays(y.StartDate, checkIn) < 0) ? (DbFunctions.DiffDays(y.StartDate, checkOut) >= 0 ? false : true) : true &&
                        (DbFunctions.DiffDays(y.StartDate, checkIn) > 0) ?  (DbFunctions.DiffDays(y.EndDate, checkIn) <= 0 ? false : true) : true
                    ))
                    .ToList();
                }

which works perfectly fine but it does not eagerly load, when i convert result of above query to my business model it again fires some queries on db.HotelRoomBlackoutPeriods

Please provide the optimum way to achieve this.
thanks in advance.
sorry (if i am asking something nonsense ) in advance

È stato utile?

Soluzione

Eager loading does not work when you change the shape of the result set. Shape of your result set is HotelRoom type. Once you use any manual join or projection, you are changing the result set and eager loading request is ignored.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top