C# - Asqueryable - Condition (Linq - Lambda)

Asked

Viewed 91 times

0

In this method, I populate my 'rule' variable with all rules coming from Select, however, I would like to filter only Status = True. How to do this?

  public async Task<BootgridResponseViewModel<ConsultaIntramexRegrasResponse>> ObterListaRegrasConsultaIntramex(BootgridRequestViewModel request)
    {
        using (var ctx = new IntramexContext())
        {
            var regras = ctx.ConsultaIntramexRegras.AsQueryable().Select(p => new ConsultaIntramexRegrasResponse
            {
                Id = p.Id,
                Descricao = p.Descricao,
                Periodicidade = p.Periodicidade,
                DiaSemanaMes = p.DiaSemanaMes,
                Horario = p.Horario,
                CodigoConsulta = p.CodigoConsulta,
                OperadorRegistroId = p.OperadorRegistroId,
                Status = p.Status,
                UltimaExecucao = p.UltimaExecucao
            }).Distinct();


            if (!string.IsNullOrWhiteSpace(request.searchPhrase))
            {
                regras = regras.Where(p => p.Descricao.Contains(request.searchPhrase)
                          || p.Periodicidade.ToString().Contains(request.searchPhrase)
                          || p.Descricao.ToString().Contains(request.searchPhrase)
                          || p.OperadorRegistroId.ToString().Contains(request.searchPhrase));
            }

            var dados = await regras.OrderByDescending(m => m.Id).AplicaFiltroAsync(request).ConfigureAwait(false);
            return dados;
        }
    }

1 answer

1


Just use the method Where with the predicate Status, as its value is a boolean do not need to compare with another boolean.

var regras = ctx.ConsultaIntramexRegras.Where(r => r.Status)
                                       .Select(p => new ConsultaIntramexRegrasResponse { })

// Continuar o código normalmente
  • Perfect @LINQ, Tks!

Browser other questions tagged

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