0
In the method below, I return some records of my entity Consultintramexrules, where it has relation with the . INCLUDE ("Emails and Parameters"). Aware that EF does not allow filtering in . INCLUDE, I would like to check how to filter these entities?
return ctx.ConsultaIntramexRegras
.Include("Emails")
.Include("Parametros")
.FirstOrDefault(p => p.Id == id);
I need it filtered as follows:
var regras = ctx.ConsultaIntramexRegras.FirstOrDefault(p => p.Id == id);
var emails = ctx.ConsultaIntramexRegrasEmails.FirstOrDefault(p => p.Id == id && p.Status);
var parametros = ctx.ConsultaIntramexRegrasParametros.FirstOrDefault(p => p.Id == id && p.Status == 1);
The Models:
public class ConsultaIntramexRegras : EntityBase<int, string>
{
public string Descricao { get; set; }
public int Periodicidade { get; set; }
public int DiaSemanaMes { get; set; }
public int Horario { get; set; }
public bool Status { get; set; }
public int CodigoConsulta { get; set; }
public virtual ICollection<ConsultaIntramexRegrasEmails> Emails{ get; set; }
public virtual ICollection<ConsultaIntramexRegrasParamentros> Parametros { get; set; }
}
public class ConsultaIntramexRegrasEmails : EntityBase<int, string>
{
public string Emails { get; set; }
public bool Status { get; set; }
public int RegrasID { get; set; }
public virtual ConsultaIntramexRegras Regras { get; set; }
}
public class ConsultaIntramexRegrasParamentros : EntityBase<int, string>
{
public int RegrasId { get; set; }
public string Descricao { get; set; }
public int ParametroId { get; set; }
public int SequenciaParametro { get; set; }
public int Tipo { get; set; }
public string Valor { get; set; }
public string TrataData { get; set; }
public int Status { get; set; }
public virtual ConsultaIntramexRegras Regras { get; set; }
}
You can add the 3 models in the question?
– Barbetta
Sure! Added.
– Bruno Souza
https://stackoverflow.com/questions/39636952/how-to-filter-include-entities-in-entity-framework. related.
– novic
If it’s in the Core version: this one has this: https://answall.com/questions/276603/returns-de-consulta-com-o-linq-em-um-reposit%C3%B3rio-Ef-core/276624#276624
– novic
@Virgilionovic thank you for the examples. I wanted to avoid using third party libraries. As a stopgap measure I did as follows.
– Bruno Souza