[英].Net 6 Api Generic query sorting based on columnName type and order Asc Desc
I'm trying to gain some experience with .net 6 apis and I have designed a simple app in which the user can sort the data based on a specific column like name or created.我正在尝试获得 .net 6 api 的一些经验,并且我设计了一个简单的应用程序,用户可以在其中根据名称或创建的特定列对数据进行排序。
But now I also want to sort based on a status that can be active or inactive (stored in DB stored as a bool).但现在我还想根据可以是活动或非活动的状态进行排序(存储在以布尔形式存储的数据库中)。
So, I would like, if possible, to have a generic method to apply sorting also by including boolean conditions like:所以,如果可能的话,我想有一个通用的方法来应用排序,也包括 boolean 条件,例如:
query = query.OrderBy(u=> u.status==false/true)
Any help would be highly appreciated.任何帮助将不胜感激。
OrderBy
already does that for you. OrderBy
已经为您做到了。
In case you want to order by false
just write the following:如果您想按
false
订购,只需写下以下内容:
query = query.OrderBy(u => u.status)
This will bring the items with status false
to the front, and move the ones with true
to the end.这会将状态为
false
的项目带到前面,并将状态为true
的项目移到最后。
In case you want to do the opposite and bring the true
-s to the front, use OrderByDescending
如果您想做相反的事情并将
true
-s 放在前面,请使用OrderByDescending
query = query.OrderByDescending(u => u.status)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.