locked
Using a xref table to filter results RRS feed

  • Question

  • User1029764681 posted

    I have 3 entities.  

    Hotel
    Airport
    HotelAirportXref

    I am trying to pull back a list of hotels linked to a specific Airport.  Simple in SQL , just my first time doing with EF and LINQ.

                VM._Hotel = db.Hotels                
                    .Include(i => i.HotelAirportXrefs)
                    //.Where(i => i.HotelAirportXrefs.AirportID==12)
                    .Where(x => x.HotelTypeID != 4)
                    .Where(x => x.bDeleted==false)
                    .ToList();

    Wednesday, July 3, 2019 12:45 PM

All replies

  • User1029764681 posted

    I ended up getting this to work this way.  Is there a better way?

      var HotelIDs = (from p in db.HotelAirportXrefs where p.AirportID==AirportID select p.HotelID).ToList();
    
                VM._Hotel = db.Hotels                
                    .Include(i => i.HotelAirportXrefs)
                    .Where(x => x.HotelTypeID != 4)
                    .Where(x => x.bDeleted==false)
                    .Where(x => HotelIDs.Contains(x.HotelID))
                    .ToList();

    I dont need to include this do I?  .Include(i => i.HotelAirportXrefs)

    Wednesday, July 3, 2019 1:09 PM
  • User1520731567 posted

    Hi fmrock164,

    I dont need to include this do I?  .Include(i => i.HotelAirportXrefs)

    It depends on your schema, your environment, your data, and many other factors.

    Include is an example of eager loading, where as you not only load the entities you are querying for, but also all related entities.

    (ask youself do you need the related data HotelAirportXrefs?)

    When using Include(), you get the benefit of loading all of your data in a single call to the underlying data store.

    If this is a remote SQL Server, for example, that can be a major performance boost.

    On the other hand,Include() queries tend to get really complicated,EF will generate very heavily nested queries using sub-SELECT and APPLY statements to get the data you want.

    More details,you could refer to:

    https://stackoverflow.com/a/19319663

    Best Regards.

    Yuki Tao

    Thursday, July 4, 2019 9:33 AM