C# - Filter Include - Context

Asked

Viewed 173 times

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?

  • Sure! Added.

  • 1

    https://stackoverflow.com/questions/39636952/how-to-filter-include-entities-in-entity-framework. related.

  • 1

    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

  • @Virgilionovic thank you for the examples. I wanted to avoid using third party libraries. As a stopgap measure I did as follows.

2 answers

1


I made a model here that has the option "where" in the 3 models and returns ConsultaIntramexRegras.

public ConsultaIntramexRegras obterRegraConsultaIntramexPorId(int id)
{
    ConsultaIntramexRegras regra;
    using (var ctx = new IntramexContext())
    {
        regra = ctx.Set<ConsultaIntramexRegrasParamentros>()
            .Where(p => p.ParametroId == 1)//Where em ConsultaIntramexRegrasParamentros
            .Include(p => p.Regras)
            .Select(p => p.Regras)
            .Where(p => p.Periodicidade == 1)//Where em ConsultaIntramexRegras
            .Include(p => p.Emails)
            .SelectMany(p => p.Emails)
            .Where(p => p.Emails == "")//Where em ConsultaIntramexRegrasEmails
            .Select(p => p.Regras)
            .FirstOrDefault();
    }

    return regra;
}
  • @Brunosouza What’s the problem? Imagine, you don’t have to apologize.

  • 1

    @Brunosouza edited my answer, see Agra

  • 1

    @Brunosouza set again, mals send wrong, boss arrived in the room

  • 1

    @Brunosouza happy to help. BS: then delete the answers you gave, if not the staff will negativate, they should be issues of the question, hug

0

I resolved it as follows. I avoided the use of third party libraries. It was not the best way, I believe there are other more effective. I will be in search and return if I find simpler solution!

    public ConsultaIntramexRegras obterRegraConsultaIntramexPorId(int id)
    {

        List<ConsultaIntramexRegras> regra;

        using (var ctx = new IntramexContext())
        {
            regra = ctx.ConsultaIntramexRegras
                .Include(p => p.Emails)
                .Include(p => p.Parametros)
                .Where(p => p.Id == id).ToList();
        }

        return regra.Select(p => new ConsultaIntramexRegras
                    {
                        CodigoConsulta = p.CodigoConsulta,
                        DataRegistro = p.DataRegistro,
                        Descricao = p.Descricao,
                        Id = p.Id,
                        Parametros = p.Parametros.Where(s => s.Status == 1).ToList(),
                        Emails = p.Emails.Where(s => s.Status == true).ToList(),
                        Status = p.Status,
                        OperadorRegistroId = p.OperadorRegistroId,
                        DiaSemanaMes = p.DiaSemanaMes,
                        Horario = p.Horario,
                        Periodicidade = p.Periodicidade
                    }).FirstOrDefault();
    }
  • It is a way to make work with the data in memory, but, that brings low performance, because, moreover, brings what you do not need ... ! ... Always try to bring from your base what you need, one of the bottleneck of development is application relation and information repository. The package is excellent is well used by the world . NET for EF 6x, while the Core version already exists how to do this.

  • @Virgilionovic you’re right. I’ll use it in next projects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.