Open joseluisct opened 11 months ago
I think the non-async version could be added to TableQuery pretty easily:
public bool Any()
{
var selectCommand = Take(1).GenerateCommand("1");
selectCommand.CommandText = $"select exists({selectCommand.CommandText})";
return selectCommand.ExecuteScalar<bool>();
}
public bool Any(Expression<Func<T, bool>> predExpr)
{
return Where(predExpr).Any();
}
Hello, I have not found an ORM direct way of checking if any row exists in a table such as AnyAsync() of Linq/Entity Framework.
Before I was using FirstOrDefaultAsync() != null or CountAsync() > 0, but I found that with Sqlite there is an efficient way of querying it as:
$"SELECT EXISTS(SELECT 1 FROM {table} WHERE {condition} LIMIT 1)"
If would be nice to have an AnyAsync() method inside the library to use as the other ones. For now I have created an extension method to use on my projects doing so, I write it here in case it can be useful for someone:
To use the extension method from AsyncTableQuery:
bool exist = await connection.Table<Agent>().AnyAsync(x => x.Name == name);
I wish more functionality of Linq integration like this could be added into the library. You can also check my other extension methods here: https://github.com/praeclarum/sqlite-net/issues/1180#issuecomment-1797078233