Add values to a C#list

Asked

Viewed 3,552 times

-1

Hi! I am reading values from a table and inserting them into a list. What I have is this:

Expiração.Add(rdr["Name" + "Address"].ToString());
Expiração.Add(rdr["Address"].ToString());
Expiração.Add(rdr["PostalCode"].ToString());
Expiração.Add(rdr["PostalCodeDesc"].ToString());
Expiração.Add(rdr["NIF"].ToString());
Expiração.Add(rdr["eMail"].ToString());
Expiração.Add(rdr["Phone"].ToString());
Expiração.Add(rdr["ContactPerson"].ToString());
Expiração.Add(rdr["Machine"].ToString());
Expiração.Add(rdr["RegistrationDate"].ToString());

But so I’m adding several items to the list, what I wanted was to add just one item with all that information, something like that (just example, why doesn’t it work):

Expiração.Add(rdr["Name" + "Address" + "PostalCode" + ...].ToString());
  • What you have to do is create a class that is composed of the table columns. From there you create a List of this class: ex: List<Classname>. If you have any questions about the code to be done, say. Ps: Entityframework search

2 answers

2

You could have an object: Expiration, which would have all the properties that Voce needs. Something like:

public class Expiracao(){
    public string Name {get; set;}
    public string Address {get; set;}
    public string PostalCode {get; set;}
    public string PostalCodeDesc {get; set;}
    public string NIF {get; set;}
    public string Email {get; set;}
    public string Phone {get; set;}
    public string ContactPerson {get; set;}
    public string Machine {get; set;}
    public string RegistrationDate {get; set;}
}

And, imagining that your rdr is an obj that implements: IDataReader, would be something like:

var objExpiracao = new Expiracao();
objExpiracao.Name = rdr["Name"].ToString();
objExpiracao.Address = rdr["Address"].ToString();
objExpiracao.PostalCode = rdr["PostalCode"].ToString();
...

Or instead of searching by name, you could search by index, something like: rdr[0], according to the selection of your instruction - SELECT Name, Addres FROM TB_Expiracao

And so for all the properties of the object you want popular. You can still add this object to a list of objects: Expiration:

var lExpiracao = new List<Expiracao>();
lExpiracao.Add(objExpiracao);

To access the data of your object, you will use: object.property:

console.WriteLine(objExpiracao.Name);

0

What you can do is create a reading method to assist you with this table data one way to do this is by using the params to pass the columns separated by ,:

   private List<string> LerDados(DbDataReader reader,params string[] colunas)
    {
        List<string> list = new List<string>();
        foreach (var col in colunas)
        {
            if (!string.IsNullOrEmpty(reader[col].ToString()))
                list.Add(reader[col].ToString());
        }
        return list;

    }

Or you can use the extended class concept

public static class Extend
{
    public static List<string> ToList(this DbDataReader reader, params string[] colunas)
    {
        List<string> list = new List<string>();
        foreach (var col in colunas)
        {
            if (!string.IsNullOrEmpty(reader[col].ToString()))
                list.Add(reader[col].ToString());
        }
        return list;

    }
}

To use:

while (reader.Read())
{
    List<string> Expiração = reader.ToList("Name", "Address", "PostalCode");
}
  • The user who declined could explain the reason or point out where the error is, so that I can make the necessary corrections or if that’s the case remove my answer.

  • 1

    Lately this is happening... the crowd comes and negative but says nothing... complicated saw.

  • So, I don’t know if my answer is wrong, it seems to me that answers what the DC asked, but I can err and my answer is completely wrong, but I need to know to do the right action.

Browser other questions tagged

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