android-Room DAO按ASC或DESC排序
作者:互联网
我正在尝试在我的@Dao接口中创建一个具有布尔参数isAsc的@Query函数以确定顺序:
@Query("SELECT * FROM Persons ORDER BY first_name (:isAsc ? ASC : DESC)")
List<Person> getPersonsAlphabetically(boolean isAsc);
显然,这是不允许的.这附近有工作吗?
编辑:
使用两个查询(如下)似乎很奇怪,因为唯一的区别是ASC和DESC:
@Query("SELECT * FROM Persons ORDER BY last_name ASC")
List<Person> getPersonsSortByAscLastName();
@Query("SELECT * FROM Persons ORDER BY last_name DESC")
List<Person> getPersonsSortByDescLastName();
解决方法:
使用CASE Expression for SQLite在Room DAO中实现此目的,
@Query("SELECT * FROM Persons ORDER BY CASE WHEN :isAsc = 1 THEN first_name END ASC, CASE WHEN :isAsc = 0 THEN first_name END DESC")
List<Person> getPersonsAlphabetically(boolean isAsc);
标签:android-room,dao,android 来源: https://codeday.me/bug/20191108/2006055.html