Search code examples
c#sqlitewindows-phone-8

get max value of column


I'm trying to get more value from a column in a table. My table is very simple:

public class MensajeTablon
{
    [PrimaryKey]
    public int id { get; set; }
    public int identificadorUsuario { get; set; }
    public string nombre { get; set; }
    public string mensaje { get; set; }
    public string foto { get; set; }
    public DateTime fecha { get; set; }
    public int identificadorTablon { get; set; }
}

Now I have 2 rows in my table, with id 1 and one with id 2. When I try to query it returns me an object with id = 0. I do not understand why. My query is:

var idMensaje = dbConn.Query<MensajeTablon>("select MAX(id) from MensajeTablon;");

I'm sure I have those two rows that if I read the table (select * from MensajeTablon) I get the two rows.


Solution

  • The only way I know how to do this is to create a Class that mirrors the results as well. So


    // the class that represents the result you want
    public class MaxId
    {
        public int id { get; set; }
    }
    

    // now you need to cast it to an "id" as well....
    var idMensaje = dbConn.Query<MaxId>("select MAX(id) AS id from MensajeTablon");
    

    Now idMensaje[0].id is your Max


    enter image description here

    var existing is the result of my query