asp.net core odata orderby complex Conditional sorting

SuperMMC 0 Reputation points
2023-06-01T09:14:09.06+00:00

asp.net core odata order with two where,how write url

1.summay.contains
2.week

		[HttpGet]
        [EnableQuery(EnsureStableOrdering = false)]
        public IActionResult Get()
        {
            IEnumerable<WeatherForecast> values = new List<WeatherForecast>()
            {
                new WeatherForecast() { Summary="cloudyday",TemperatureC=21,Week=3} ,
                new WeatherForecast() { Summary="rain",TemperatureC=23,Week=2} ,
                new WeatherForecast() { Summary="sunny",TemperatureC=22,Week=1} ,
                new WeatherForecast() { Summary="sunny1",TemperatureC=25,Week=1} ,
            };
 
            values = values
                .OrderBy(f=>f.Summary.Contains('s'))
                .ThenBy(f=>f.Week)
                .AsQueryable();
			// use url, no should use server side 
			// Alternatively, use sorting on the service side first, and then use the orderby parameter in the URL (ThenBy)
 
            return Ok(values);
        }


public class WeatherForecast
    {
        public DateTime Date { get; set; }
 
        [Key]
        public int TemperatureC { get; set; }
 
        public int Week { get; set; }
 
        public int TemperatureF { get; set; }
 
        public string? Summary { get; set; }
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,162 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Chen Li - MSFT 1,221 Reputation points
    2023-06-02T06:16:34.6633333+00:00

    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

    User's image


    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