last record entity C # windows form

0

Hello I hope you can help me I'm trying to get how many records my sql table has I have the following

public int  ultimoRegistro()
    {
        var LastRecord = (from c in db.Alumnos
                          orderby c.IDAlumno descending
                          select c).First();
        return LastRecord;
    }

but I get an error in the data types

    
asked by Ariel Alvarenga 09.11.2018 в 23:39
source

3 answers

3

If I understand correctly, you want to get the last IDAlumno stored in the Alumnos table.

The problem with your code is that it is returning the entire entity Alumno , so it would be necessary to define which property to return.

Therefore, I return select c.IDAlumno .

For this, make the following code:

public int ultimoRegistro()
{
    var LastRecord = (from c in db.Alumnos
                      orderby c.IDAlumno descending
                      select c.IDAlumno).First();
    return LastRecord;
}
    
answered by 10.11.2018 / 00:03
source
0

If you use EntityFramework, you can use this:

db.Alumnos.Max();

so you will get the last record by the Id.

    
answered by 10.11.2018 в 00:37
0

To obtain the number of records in your table, you have to use the Count method in the following way:

public int  ultimoRegistro()
{
    var LastRecord = (from c in db.Alumnos
                      select c).Count();

    return LastRecord;
}
    
answered by 11.11.2018 в 02:01