Add values to a list C #

-1

Hello! 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 that whole information, something like this (just an example, because that does not work):

Expiração.Add(rdr["Name" + "Address" + "PostalCode" + ...].ToString());
    
asked by anonymous 16.06.2017 / 11:06

2 answers

2

You could have an object: Expiration, which would have all the properties you need. 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, assuming your rdr is an obj that implements: IDataReader , it would look 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 statement - 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);
    
16.06.2017 / 13:08
0

What you can do is to create a read method to help you with this table data. One way to do this is to use params to pass 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 concept of the extended class

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");
}
    
16.06.2017 / 13:08