locked
Dynamic LINQ in Asp Core 3.1 RRS feed

  • Question

  • User439975351 posted

    Hi all,

    I'm trying to implement dynamic OrderBy using Dynamic LINQ in an Asp Core 3.1 project, here's what I have:

    orderedList = filters.SortDirection.ToLower(CultureInfo.CurrentCulture) == "asc"
                        ? orderedList.OrderBy(filters.SortBy).ToList()
                        : orderedList.OrderByDescending(filters.SortBy).ToList();

    I note that on the Nuget page (https://www.nuget.org/packages/System.Linq.Dynamic.Core/) it specifies Asp Core 2.1, does anyone know if this can or cannot be used in 3.1 and if not is there an alternative way of achieving this in 3.1?

    Wednesday, May 6, 2020 3:19 PM

Answers

  • User439975351 posted

    Thanks all.

    The answer to my question, which was specifically regarding the Dynamic Linq Nuget package rather than anything else, was to construct the OrderBy as follows:

    orderedList = filters.SortDirection.ToLower(CultureInfo.CurrentCulture) == "asc"
                        ? orderedList.OrderBy(filters.SortBy).ToList()
                        : orderedList.OrderBy($"{filters.SortBy} Desc").ToList();

    You cannot use OrderByDescending in this context.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 1:49 PM

All replies

  • User303363814 posted

    Have you tried using the code?  If it gives the answer you want then the answer is 'yes'.  If it doesn't then the answer is 'no'.

    (The statement is so basic that it looks to me it would work in any version forever in the past and forever in the future - but why take the guess of some random dude on the internet.  Run it and test)

    Wednesday, May 6, 2020 11:35 PM
  • User439975351 posted

    Thanks PaulTheSmith,

    Yep, tried that but get build errors. I have added the nuget package and usings. It looks like it could be that OrderByDescending is not supported which is just strange. It builds with just OrderBy...

    Thursday, May 7, 2020 7:20 AM
  • User711641945 posted

    Hi 1jus,

    Could you share more code about what is your orderedlist and filters?By your code,we could not understand how did you use System.Linq.Dynamic.Core package.

    Best Regards,

    Rena

    Thursday, May 7, 2020 7:56 AM
  • User303363814 posted

    get build errors

    If you take your car to a motor mechanic would you make them guess what your problem is???

    If you have an error, then show use the exact error, where it occurs and the surrounding code!

    Thursday, May 7, 2020 1:05 PM
  • User439975351 posted

    Thanks all.

    The answer to my question, which was specifically regarding the Dynamic Linq Nuget package rather than anything else, was to construct the OrderBy as follows:

    orderedList = filters.SortDirection.ToLower(CultureInfo.CurrentCulture) == "asc"
                        ? orderedList.OrderBy(filters.SortBy).ToList()
                        : orderedList.OrderBy($"{filters.SortBy} Desc").ToList();

    You cannot use OrderByDescending in this context.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2020 1:49 PM