Hi @SuperMMC,
orderby
supports sorting a field, but it cannot only sort columns that contain a certain character in the field. So .OrderBy(f=>f.Summary.Contains('s'))
is actually an invalid operation.
Similarly, OData
supports operations such as filtering and sorting, but it cannot sort Summay.contains
.
I suggest that you can sort the Summary
field containing s
in the backend using bubble sorting, and then use the URL to sort the Week
field. Like this:
[HttpGet]
[EnableQuery(EnsureStableOrdering = true)]
public IActionResult Get()
{
IList<WeatherForecast> values = new List<WeatherForecast>()
{
new WeatherForecast() { Summary="cloudyday",TemperatureC=21,Week=3} ,
new WeatherForecast() { Summary="rain",TemperatureC=23,Week=2} ,
new WeatherForecast() { Summary="sunny1",TemperatureC=22,Week=1} ,
new WeatherForecast() { Summary="sunny",TemperatureC=25,Week=1} ,
};
for (var i = 0; i < values.Count(); i++)
{
for (var j = 0; j < values.Count() - i; j++)
{
var weatherForecast = new WeatherForecast();
if (values[i].Summary.Contains('s'))
{
if (values[i + j].Summary.Contains('s'))
{
if (string.Compare(values[i].Summary, values[i + j].Summary) == 1)
{
weatherForecast = values[i];
values[i] = values[i + j];
values[i + j] = weatherForecast;
}
}
}
}
}
return Ok(values);
}
Test Result:
Url: Your Url + ?$orderby=Week
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Chen Li