Home >Backend Development >C++ >How Can I Dynamically Order IEnumerable and IQueryable Using String Expressions?

How Can I Dynamically Order IEnumerable and IQueryable Using String Expressions?

Susan Sarandon
Susan SarandonOriginal
2025-02-02 22:31:10894browse

How Can I Dynamically Order IEnumerable and IQueryable Using String Expressions?

Dynamic sorting IenumeRable

/ iQueryable Original problem:

When searching for dynamic linq examples, I found that a method can use SQL string (e.g., "Orderby (" Name, Age Desc ")) to sort the IQueryable instance. Is there a way to extend this function to Ienumeration

?

Answer:

In order to achieve this without using the Dynamic Linq library, the following code fragment provides core expression logic:

This code allows it to sort it by packaging Ienumeration

in asquile ().

<code class="language-C#">public static IOrderedQueryable<T> OrderBy<T>(
    this IQueryable<T> source,
    string property)
{
    return ApplyOrder<T>(source, property, "OrderBy");
}

// ...省略其余代码以简洁起见...</code>
Dynamic Linq integrated extension:

Expand this function to dynamic linq:

This code provides dynamic sorting functions in Linq to Objects.

The above is the detailed content of How Can I Dynamically Order IEnumerable and IQueryable Using String Expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn