0

I have an object like this:

public class VendorHotelRoom : BaseEntity
{
    public virtual ICollection<VendorHotelRoomName> VendorHotelRoomNames { get; set; }
    public bool CompleteVendorHotelRoomNames { get; set; }

    public Guid VendorHotelRoomDetailId { get; set; }
    public virtual VendorHotelRoomDetail VendorHotelRoomDetail { get; set; }
    public bool CompleteVendorHotelRoomDetail { get; set; }

    public virtual ICollection<VendorHotelRoomAvailability> VendorHotelRoomAvailabilities { get; set; }
    public bool CompleteVendorHotelRoomAvailabilities { get; set; }

    public virtual ICollection<VendorHotelRoomFacility> VendorHotelRoomFacilities { get; set; }
    public bool CompleteVendorHotelRoomFacilities { get; set; }

    public virtual ICollection<VendorHotelRoomDescription> VendorHotelRoomDescriptions { get; set; }
    public bool CompleteVendorHotelRoomDescriptions { get; set; }
}

I want to include objects conditionally based in boolean properties like this

  var vhr = hotelContext.VendorHotelRooms.Where(c => c.Id == VendorHotelRoomId)
       //if CompleteVendorHotelRoomAvailabilities true .Include(c => c.VendorHotelRoomAvailabilities )
       //if condition(boolean) true     .Include(c => c.VendorHotelRoomDescriptions)
       //if condition(boolean) true     .Include(c => c.VendorHotelRoomDetail)
       //if condition(boolean) true     .Include(c => c.VendorHotelRoomFacilities)
       //if condition(boolean) true     .Include(c => c.VendorHotelRoomNames).FirstOrDefault();

Is there any best practice way to avoid if and else?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
zolfaghari
  • 192
  • 3
  • 14
  • 1
    What's wrong with `if`'s? Also are those properties (`CompleteVendorHotelRoomNames`, etc.) actually columns in the database? – Guru Stron Dec 11 '22 at 07:29
  • @GuruStron on if and else we must handle all of setuations such as if (com1 and comp 2 and comp 3 ) and else and else and else and YES the complete... properties is in database – zolfaghari Dec 11 '22 at 07:55
  • 1
    Not possible if the include controlling properties are contained in the entity as in your example (because `Include` is at query level, i.e. before the query execution, hence at that point it has no access to entity properties simply because there are no entity instances). If the include controlling properties are external and accessible during the query creation, then this is duplicate of https://stackoverflow.com/questions/53474431/ef-core-linq-and-conditional-include-and-theninclude-problem/53476825#53476825 – Ivan Stoev Dec 11 '22 at 07:58
  • i want to impliment way just like above //if condition(boolean) true .Include(c => c.VendorHotelRoomDescriptions) but in one line not thosends of if else – zolfaghari Dec 11 '22 at 07:58
  • 2
    In other words, `Include` / `ThenInclude` are EF Core methods for *eager* loading related data, that's why they cannot use conditions based on entities. In your case, the only option you have is iterating the query result and *explcitly* loading the related data needed. Which will introduce executing a lot of additional db queries similar to *lazy* loading. Some helper methods could be made to make it easier for use, but unfortunately they must operate on `IEnumerable` rather than `IQueryable`. – Ivan Stoev Dec 11 '22 at 08:12
  • 1
    This is very specific so there's no general EF solution. But you don't need to load each related table in separate steps. You just load the entity once without any related data and then do a second load with all the related data based on the bools you loaded the first time. Note, if you need to load multiple entities simultaneously, this will not be efficient. Just load all related data in that case – JHBonarius Dec 11 '22 at 08:17
  • 1
    If you really have "thousands" of conditions there's something profoundly wrong with your code and you better show the bigger picture. I have a strong feeling your problem is at a higher level (aka [XY problem](https://meta.stackexchange.com/q/66377/168269)). But even if you were exaggerating, this is not a healthy pattern. The number of Includes in a query should always be limited and parametrizing them makes the runtime performance highly unpredictable and the code untestable. – Gert Arnold Dec 11 '22 at 08:57
  • Maybe you can try to use `where` inside `Include`.You can try to refer to the [link](https://stackoverflow.com/questions/43618096/filtering-on-include-in-ef-core). – Yiyi You Dec 12 '22 at 07:57

0 Answers0