fedeteka
fedeteka

Reputation: 963

Foreach loading fields values of and object

Learning with C#, SQlite and Entity Framework.

Class Usuario

public partial class Usuario
{
    private DbSet<Usuario> usuarios;

    public Usuario(DbSet<Usuario> usuarios)
    {
        this.usuarios = usuarios;
    }

    public long Id { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public string Reminder { get; set; }
}

I want to loop trough all the registries on the table Usuario and show some fields concatenated on a MessageBox (console will be the same)

My code:

using (var db = new MyBDEntities())
{
    foreach (Usuario usuarios in db.Usuarios)
    {
        Usuario ActualUser = new Usuario();

        var nameActualUser = ActualUser.Name;
        var passActualUser = ActualUser.Password;
        var reminderActualUser = ActualUser.Reminder;

        MessageBox.Show("Reading info from: " + nameActualUser + " - "  + passActualUser + " - " + reminderActualUser);
    }
}

But I know I miss something on the line

Usuario ActualUser = new Usuario();

because Visual Studio says

There is no argument given that corresponds to the required formal parameter 'usuarios'

I don't know how to "load" the full registry / Object into a new one to handle the fields. Thanks

Upvotes: 0

Views: 112

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51683

Using the actual iteration variable should solve your problem:

foreach (Usuario usuarios in db.Usuarios)
{ 
    var nameActualUser = usuarios.Name;
    var passActualUser = usuarios.Password;
    var reminderActualUser = usuarios.Reminder;

    MessageBox.Show("Reading info from: " + nameActualUser + " - "  + passActualUser + " - " + reminderActualUser); 
}

If you need the data elsewhere, you could instanciate a list outside of the loop and stuff the needed users inside it...

Upvotes: 2

Related Questions