Limit index query but not combo query

Asked

Viewed 86 times

2

Good afternoon,

In the company’s ERP we have a query in the Index that is limited to 80 records. The problem is that in create we have combos that bring more than 150 records and is also limiting to 80 records.

How can I leave only on index without having to create another method?

private StringBuilder LimitarRetornoLinhas()
    {
        var sql = new StringBuilder();
        sql.Append("SELECT * FROM (" + Environment.NewLine);
        sql.Append(_sql.ToString());
        sql.Append(" ) WHERE ROWNUM <= 80 ");
        return sql;
    }

-

public List<Fabricante> Selecionar()

    {
        var fabricantes = new List<Fabricante>();

        _sql.Append(" ORDER BY FAB_NOME ");

        StringBuilder sql = LimitarRetornoLinhas();
        Conexao conexao = new Conexao();
        DataTable dt = new DataTable();
        conexao.AbrirConexao();

        dt = conexao.ObterDataTable(sql.ToString());

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                fabricantes.Add(Mapear(dt.Rows[i]));
            }
        }

        return fabricantes;
    }
  • because it does not parameterize the method with the desired limit?

2 answers

1


Change to receive the desired number of records as parameter:

private StringBuilder LimitarRetornoLinhas(int quantidadeRegistros = 80)
{
    StringBuilder sql = new StringBuilder();

    sql.Append("SELECT * FROM (" + Environment.NewLine);
    sql.Append(_sql.ToString());
    sql.Append(string.Format(" ) WHERE ROWNUM <= {0} ", quantidadeRegistros));

    return sql;
}

Now just call your method by passing the desired amount of records:

StringBuilder query = LimitarRetornoLinhas(150);
  • I would only leave a default value... not to impact other places where the method may be being invoked and if you want the default already defined in the business rule

  • Very well noted, I will change the answer. Thank you!

  • Thank you. Opened my mind.

  • I did it in a different way because I would set a value. It would not be interesting to me.

0

private StringBuilder LimitarRetornoLinhas(bool carregarTodos)
    {
        var sql = new StringBuilder();
        if (carregarTodos == false)
        {
            sql.Append("SELECT * FROM (" + Environment.NewLine);
            sql.Append(_sql.ToString());
            sql.Append($" ) WHERE ROWNUM <= 80"); 
        }
        else
        {
            sql.Append(_sql.ToString());
        }

        return sql;
    }

-

public List<Fabricante> Selecionar(bool carregarTodos)
    {
        var fabricantes = new List<Fabricante>();

        _sql.Append(" ORDER BY FAB_NOME ");

        StringBuilder sql = LimitarRetornoLinhas(carregarTodos);
        Conexao conexao = new Conexao();
        DataTable dt = new DataTable();
        conexao.AbrirConexao();

        dt = conexao.ObterDataTable(sql.ToString());

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                fabricantes.Add(Mapear(dt.Rows[i]));
            }
        }

        return fabricantes;
    }
  • But if the loadAll is true Bring no one?

  • I pass as true in the registration controller that pulls the information from this repository and it brings all manufacturers and in index limits in 80 records.

Browser other questions tagged

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