sqlite 获取列的最大值

huus2vyu  于 2023-03-03  发布在  SQLite
关注(0)|答案(1)|浏览(128)

我试图从表中的一列获得更多的值。我的表非常简单:

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; }
}

现在我的表中有2行,ID为1,一行ID为2。当我尝试查询时,它返回一个ID = 0的对象。我不明白为什么。我的查询是:

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

我确信我有这两行,如果我读表(从MensajeTablon中选择 *),我会得到这两行。

bogh5gae

bogh5gae1#

我知道的唯一方法是创建一个类来镜像结果。所以

// 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");

现在idMensaje[0].id是您的最大值

var existing是我的查询结果

相关问题