0

I am trying to dynamically set an order by in Linq with the following code:

        Func<TransactionResult, Object> orderByFunc = null;

            var sortKey = query.SortOption.Split(":")[0];
            var sortDirection = query.SortOption.Split(":")[1];

            if (sortKey == "Date")
                orderByFunc = item => item.Created;
            else if (sortKey == "PatientName")
                orderByFunc = item => item.Customer.FirstName;
            else if (sortKey == "Status")
                orderByFunc = item => item.Status;
            else if (sortKey == "Source")
                orderByFunc = item => item.Source;
            else if (sortKey == "Amount")
                orderByFunc = item => item.Amount;


            if (sortDirection== "asc")
            {
                tQuery = tQuery.OrderBy(orderByFunc).AsQueryable();
            }
            else
            {
                tQuery = tQuery.OrderByDescending(orderByFunc).AsQueryable();
            }

I'm getting the error : The source 'IQueryable' doesn't implement 'IAsyncEnumerable<########.Application.Transaction.Queries.Transactions.TransactionsDto>'. Only sources that implement 'IAsyncEnumerable' can be used for Entity Framework asynchronous operations.

Any help would be much appreciated, thank you.

Mardo
  • 117
  • 1
  • 9
  • It seems you are using the wrong implementation. See duplicates. If you still can't figure it out, feel free to post a new question, making sure you explain what research you've already done (including reviewing the existing duplicates), and what _specifically_ you need help with. – Peter Duniho Apr 16 '21 at 00:46
  • 1
    The actual problem is that by using `Func` you are executing the query client side, that's why there is `.AsQueryable()` calls, but the context is already switched and it is not EF queryable anymore. The solution as usual is to change `Func<...>` to `Expression>`, thus hitting the `Queryable` overloads and not leaving EF queryable context. – Ivan Stoev Apr 16 '21 at 01:15

0 Answers0