I have a scenario where I want to fetch navigation property with EF based on a specific condition.
- Want to fetch Account if its isActive=true otherwise not fetch Account.
- Want to send isActive=true then share true records ; isActive=false the share false records.
Is it possible with EF Core?
Below is the snippet.
return await _context.Contacts
.Include(x => x.PrefixTitle)
.Include(x => x.Account) // **here i want to fetch this if Account.IsActive=true**
.Include(x => x.AssignedToUser)
.Include(x=>x.LeadSource)
.Include(x=>x.EmailAddress.Where(p=>p.IsDeleted==false))
.Where(a => a.ContactId == id && a.IsDeleted == false)
.FirstOrDefaultAsync();
Account in above is Single Navigation Property not the collection, below are Contact and Account Classes for details.
public class Contact
{
public int ContactId { get; set; }
public string Department { get; set; }
public int? AccountId { get; set; }
public Account Account { get; set; } //one-many relation
}
public class Account
{
public int AccountId { get; set; }
public string Name { get; set; }
public ICollection<Contact> Contacts { get; set; } = new List<Contact>(); // One-Many relation
}
any help will be appreciated. Thanks