w0051977
w0051977

Reputation: 15817

Entity Framework: Overload resolution failed because no accessible ORDERBY can be called with these arguments

Please see the code below:

Public Function GetMembers(Optional ByVal sortExpression As String = "MemberId ASC") As List(Of Member) 'Implements IMemberDao.GetMembers
        Using context = New LibraryDBEntities()
            Dim members = context.Members.AsQueryable.OrderBy(sortExpression).ToList
            Return Mapper.Map(Of List(Of MemberEntity), List(Of Member))(members)
        End Using
    End Function

The error I get is: "Overload resolution failed because no accessible ORDERBY can be called with these arguments". I am new to Entity Framework.

Upvotes: 1

Views: 842

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727077

Entity Framework is not dynamic enough to handle sort conditions specified as strings. A call of OrderBy takes an expression, not a string. The expression takes an object, and produces an expression that maps to a column on which to sort the output:

Dim members = context.Members.AsQueryable.OrderBy(Function(member) member.MemberId).ToList

Moreover, you cannot specify if a sort is to be ascending or descending in your sort expression: for sorting in ascending order use OrderBy; for sorting in descending order use OrderByDescending.

Upvotes: 0

Oscar Paz
Oscar Paz

Reputation: 18312

OrderBy expects an Expression argument, not a string. What you should have written is:

context.Members.AsQueryable().OrderBy(m => m.MemberId);

For descending:

context.Members.AsQueryable().OrderByDescending(m => m.MemberId);

This tells Entity Framework to order by the MemberId property of the Member class (I assume the Members property is a Queryable of Member). Of course, then you have to rewrite the GetMembers() method so to translate the sortExpression you pass to it to a valid .Net Expression.

Upvotes: 0

Related Questions