dynamic order by different column and table

smje_jess 1 Reputation point
2021-05-26T20:17:58.677+00:00

Hello All,

we are trying to add dynamic Order by different column in multiple tables. we will get column name and have to sort data.
for example I want sort Department.Name or Staff=> JoiningDate . I tried different options and didn't work. not sure how to implement.

Below mentioned column need to sort.
Column Name: Department => Name
Column Name: Staff=> JoiningDate

public void getDepartment(String orderBy){
_context.Get<Department>()
           .Include(department => department.Staff); 
}
     
Developer technologies .NET Entity Framework Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-05-27T06:04:58.817+00:00

    Hi smjejess-2326,
    Have you tried the Dynamic Linq?
    Please refer to the following threads:
    Linq To SQL: Sort Query by Arbitrary Property(Column) Name
    How does one sort using Entity Framework Dynamic “SortBy” string on a complex object?
    Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.