-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement dynamic linq for Query.OrderBy and Query.Where #154
Comments
@fiseni have you done dynamic order-by at all? Seems like something that might be a common request for specifications. Thoughts? |
I provided an extension in the following issue here. |
Thanks @fiseni and @ardalis. I implemented the extension method provided by @lankymart in issue #53 and it seems to be doing exactly what I wanted. |
This does seem to be almost a duplicate of #53. I'll add an issue for us to document this in our docs. |
I was just about to ask the same question! You guys are on top of it! |
Would also love a way to add whether the order should be ascending or descending like in DynamicLINQ |
@davidhenley the solution that @lankymart listed in issue #53 uses a comma separated string value for the list of OrderBy fields and if you prefix a - in front of the string field name, for example; "Name, -City", it does the OrderBy(Name) and then OrderByDescending(City). Seems to work well for me, so far. |
Thanks @dcadlereii, I am using his extension and it is working well. Only thing that I could use now is a dictionary that maps the strings to lambda functions. Example: Would that work with something like |
I would like to be able to pass strings that represent table fields from my client into my API where I have the ardalis.Specification nuget installed. @ardalis had recommended that I review suggestions in a StackOverflow post here: https://stackoverflow.com/questions/2728340/how-can-i-do-an-orderby-with-a-dynamic-string-parameter and
reviewing the SO post led me to the System.Linq.Dynamic.Core nuget package that looked like it would allow me to achieve my goal with some extension methods.
I installed the System.Linq.Dynamic.Core nuget on my API and added the using statement in my Specification class, but then when I try to pass a string into the Query.OrderBy(stringVariable) I get the following error;
CS1929: 'ISpecificationBuilder' does not contain a definition for 'OrderBy' and the best extension method overload 'DynamicQueryableExtensions.OrderBy(IQueryable, string, params object[])' requires a receiver of type IQueryable'
Here is my current spec class;
In my use case, the client for my API will be a Blazor WASM that uses a SyncFusion Blazor Grid. The grid allows the potential for the user to sort and filter on every column. I would like to support this flexibility in my API but I don't want to have to code each possible individual table field for every table that will have its paged data exposed in a Grid component. Being able to simply pass the string representation of a table field would be a big benefit toward my desired use case if I can figure out how to correctly apply it to my spec.
Can anyone tell me if this is actually possible with the ardalis.Specification nuget and if so, recommend a way to make this work correctly and/or provide an example?
The text was updated successfully, but these errors were encountered: