question

MrEdge-3552 avatar image
0 Votes"
MrEdge-3552 asked DanielZhang-MSFT commented

How to declare OrderBy in Interface

I have an Interface using the Entity Framework 6 as part of the project with a method such as

 IEnumerable<Customer> Get;

Now I would like to introduce a sort feature. I'm aware I can have another parameter such as Sort=false and the class that implements the above method can use that to sort Asc/Desc by a column name I provide, however for each class I would have to enter the column name manually.

Is there a way I can have the Get function to accept a non mandatory parameter that allows the ability to select a column based on the table/view that is being targeted? So this way the class sorts by the column name that is within the table dynamically?

dotnet-entity-framework
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MrEdge-3552,
Maybe you can also dynamically specify Linq OrderBy parameters through reflection.
Here is a related thread you can refer to.
Best Regards,
Daniel Zhang


0 Votes 0 ·

Thanks Daniel! Whilst reading up on your advice I came across the below line of code

 IOrderedQueryable<TEntity>> orderBy

I wasn't entirely sure what this does or what it's used for, just wanted to point this out in case this might be something instead of reflection that could be used?

0 Votes 0 ·

Hi @MrEdge-3552,
The IOrderedQueryable interface represents the result of a sorting query that calls the method(s) OrderBy, OrderByDescending, ThenBy or ThenByDescending.
More details please refer to the following documents.
IOrderedQueryable Interface
LINQ: Building an IQueryable Provider - Part I
Best Regards,
Daniel Zhang


0 Votes 0 ·

0 Answers