Room DAO Order By ASC or DESC variable

前端 未结 3 2270
半阙折子戏
半阙折子戏 2021-02-18 17:48

I\'m trying to make a @Query function in my @Dao interface which has a boolean parameter, isAsc to determine the order:

@Q         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-18 18:38

    Use CASE Expression for SQLite to achieve this in 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 getPersonsAlphabetically(boolean isAsc);
    

提交回复
热议问题