I have a db table column where some properties are stored as json. Here is an example :
properties
----------
{ "SortBy" : "ASC" }
List<MyItem> items = await this.dbContext.AllItems
.Where(x => x.field.CompanyId== CompanyId)
.OrderBy(x => x.Value);
I need to change the OrderBy based on that JSON value. It can be ASC, DESC, or no OrderBy() if there is no value in there.
The sort column "properties" is in the same table as AllItems. I will need to dynamically sort the column based on properties JSON.
Any suggestions?
AllItems? 2) What database are you using? 3) How would the query look in pure SQL?.OrderBy(...)vs.OrderByDecending(...)) or do you also need to dynamically select the sorted column (.OrderBy(x => x.SomeSortByColumn))?in the same table as AllItemsyou can't use it in the same query. That's a very weird design. A SQL query can't automagically change its behavior based on data that didn't even exist when the SQL itself was compiled into an execution plan. You'll have to run one query to extract the JSON data, another to get the real data. Given how wasteful this is, if you don't have too many rows, you might as well load the data then sort in memoryAllItemscontains a company's orders, is thepropertiesJSON repeated across all orders? Even though it doesn't change?