Comments on: How to Implement Sorting in ASP.NET Core Web API https://code-maze.com/sorting-aspnet-core-webapi/ Learn. Code. Succeed. Thu, 20 Feb 2025 11:53:44 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Tanmay https://code-maze.com/sorting-aspnet-core-webapi/#comment-8566 Tue, 27 Jun 2023 15:16:54 +0000 https://code-maze.com/?p=48970#comment-8566 Check this,
if you provide an invalid single column in OrderBy you get an error stating that the entities.OrderBy(orderQuery) does not work with empty value.

URL: https://localhost:5001/owners?OrderBy=tenantId desc         

(tenantId is not a valid column in owners entity)

Exception: System.ArgumentException
Message: Value cannot be empty. (Parameter ‘ordering’)

]]>
By: Borjess https://code-maze.com/sorting-aspnet-core-webapi/#comment-7984 Mon, 24 Apr 2023 17:27:03 +0000 https://code-maze.com/?p=48970#comment-7984 Works properly. Thank you Vladimir for this excellent article.

]]>
By: Sam B https://code-maze.com/sorting-aspnet-core-webapi/#comment-7289 Thu, 05 Jan 2023 15:52:35 +0000 https://code-maze.com/?p=48970#comment-7289 In reply to Marinko Spasojević.

Thank you for responding. I had already done that except for OwnerParameters.cs, after making Name nullable there, it started working. I didn’t think it got validation from that class.

]]>
By: Marinko Spasojević https://code-maze.com/sorting-aspnet-core-webapi/#comment-7271 Wed, 04 Jan 2023 07:56:40 +0000 https://code-maze.com/?p=48970#comment-7271 In reply to Sam B.

Based on the JSON you provided, it seems that the [ApiController] attribute took over the request and validate it. After .NET 6, if you don’t want any property to be required, you can make it nullable:

public string? Name { get; set; }

If you do this, I think you won’t get this error. Of course, I would suggest you to read more about the ApiController attribute here https://code-maze.com/apicontroller-attribute-in-asp-net-core-web-api/ just to be sure what it does (if you are not familiar with it).

]]>
By: Sam B https://code-maze.com/sorting-aspnet-core-webapi/#comment-7266 Tue, 03 Jan 2023 23:42:04 +0000 https://code-maze.com/?p=48970#comment-7266 Did everything as instructed. Why is there a validation error before it even gets to the sorting code?

“title”“One or more validation errors occurred.”,
    “status”400,
“errors”: {
        “Name”: [
            “The Name field is required.”
        ]

]]>
By: Brandon https://code-maze.com/sorting-aspnet-core-webapi/#comment-6145 Tue, 26 Jul 2022 17:09:27 +0000 https://code-maze.com/?p=48970#comment-6145 In reply to ziemiot.

you need to install System.Linq.Dynamic.Core from nuget

]]>
By: ziemiot https://code-maze.com/sorting-aspnet-core-webapi/#comment-5702 Tue, 24 May 2022 11:02:06 +0000 https://code-maze.com/?p=48970#comment-5702 Does this way OrderBy work in .net> 5.0?

            return entities.OrderBy(orderQuery);

I have an exception:

The type arguments for method 'Queryable.OrderBy <TSource, TKey> (IQueryable <TSource>, Expression <Func <TSource, TKey >>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
]]>
By: Pedro https://code-maze.com/sorting-aspnet-core-webapi/#comment-5114 Tue, 15 Feb 2022 18:58:47 +0000 https://code-maze.com/?p=48970#comment-5114 Hi, first of all thanks for the tutorial.
I had a question, why this line of code:

if (!entities.Any())            return entities;

Isn’t this an unecessary roundtrip to database?

Regards

]]>
By: Ugochukwu Umerie https://code-maze.com/sorting-aspnet-core-webapi/#comment-4950 Mon, 17 Jan 2022 16:15:47 +0000 https://code-maze.com/?p=48970#comment-4950 Hello, how do you get the PropertyInfos[] of the related class(es) of a parent class like in One-To-Many, One-To-One, Many-To-Many scenerio for sorting and apply them to the dynamic linq?

]]>
By: Lukasz Pomianowski https://code-maze.com/sorting-aspnet-core-webapi/#comment-4356 Tue, 21 Sep 2021 12:59:59 +0000 https://code-maze.com/?p=48970#comment-4356 What I miss here is an option to also sort via navigation properties like
name.firstname, name.lastname
( assuming name is navigation property ).

]]>