How to know which is the last element on a list?

Asked

Viewed 2,805 times

8

I am doing a dynamic sql query, in which I use lists, my problem is, how to know which last element of this list

Follow the code made so far:

List<string> campos = new List<string>();

campos.Add("id");
campos.Add("descricao");
campos.Add("Titulo");

string sql = "INSERT INTO teste( ";

foreach (string campo in campos)
{
   sql += campo + ", ";//preciso saber o ultimo elemento para fechar o parênteses ao invés da vírgula
}
  • 1

    takes its code itself and at the end remove two characters from the sql string... sql = sql.Substring(0, sql.Length - 2);

8 answers

11

You don’t need to know the last item on the list. After the foreach take the string and do this:

//Remove a virgula e adiciona o parêntesis
sql = sql.TrimEnd(',') + ")";

String.Trimend

6

You can use the string.Join to do this.

List<string> campos = new List<string>();

campos.Add("id");
campos.Add("descricao");
campos.Add("Titulo");

string sql = "INSERT INTO teste(" + string.Join(",", campos) + ")";

Check the example on Dotnetfiddle

4

One more way:

var sql = "INSERT INTO teste( " + campos[0];
for (var i = 1; i < campos.Count; i++) {
    sql +=  ", " + campos[i];
}

or the solution I don’t like to use flag but uses foreach:

var primeiro = true;
foreach (var campo in campos) {
    sql += (primeiro ? ", " : "") + campo;
    primeiro = false;
}

I put in the Github for future reference.

There are several other ways.

Is there a chance this code has some SQL Injection problem.

2


List<string> campos = new List<string>();
campos.Add("id");
campos.Add("descricao");
campos.Add("Titulo");
string sql = "INSERT INTO teste( ";
foreach (string campo in campos)
   sql += campo + ", ";

sql = sql.Substring(0, sql.Length - 2);

1

...
string ultimoItem = "";

//verificando se há itens na lista para evitar possíveis erros
if (campos.Count > 0) {
    ultimoItem = campos[campos.Count - 1];
}

1

I believe that using the for in place of the foreach would be easier

I don’t know enough about the language so I don’t know if it’s.size() fields or.length() fields or fields. Count

List<string> campos = new List<string>();

campos.Add("id");
campos.Add("descricao");
campos.Add("Titulo");

string sql = "INSERT INTO teste( ";
int quantidade = campos.Count;
for(int i = 0; i < quantidade; i++)
{
   string campo = campos[i];
   if((quantidade  - 1) == i) {
       sql += campo + ") ";// é o ultimo
   } else {
       sql += campo + ", ";// Não é o ultimo
   }
}
  • At first I used this way, but still using the foreach.

  • 1

    this, da to do in several ways. could create a variable before the foreach and go incrementing it

0

var pos = fields[fields. Count - 1]; //last list elelment

0

I always do it this way. It only works if your collection is indexable (ILIST)

Include using System.Linq;

List<string> campos = new List<string>();

campos.Add("id");
campos.Add("descricao");
campos.Add("Titulo");

string sql = "INSERT INTO teste( ";

for (int i = 0; i< campos.length -1; i++)
{
   var campo = campos[i]
   sql += campo + ", ";
}

sql += campos.Last() + ");

Browser other questions tagged

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