OrderBy Linq.Expression as parameter = (Of Func(Of T,IComparable)) to perform LinqToEntity is not working

Posted by NicoJuicy on Stack Overflow See other posts from Stack Overflow or by NicoJuicy
Published on 2012-04-04T15:21:13Z Indexed on 2012/04/04 17:28 UTC
Read the original article Hit count: 300

Filed under:
|
|
|

I'd like to get this working:

Call: (Count & Page are used for pagination, so Count = 20 and Page = 1 for example, for the first 20 values). Sorting should be by name

LeverancierService.GetLeveranciers(Function(el) el.Name, Count, Page)

Equivalent in c#: LeverancierService.GetLeveranciers(el=> el.Name, Count, Page)

Method that gives an error (parameters shown above):

 Public Overridable Function GetAllPaged(orderby As Expression(Of Func(Of T, IComparable)), ByVal Count As Integer, ByVal Page As Integer) As IEnumerable(Of T)
      Return dbset.OrderBy(orderby).Skip((Page - 1) * Count).Take(Count).ToList()
 End Function

Already tried changing it to this, but it gives the same error:

 Public Overridable Function GetAllPaged(Of TOrderBy)(orderby As Expression(Of Func(Of T, TOrderBy)), ByVal Count As Integer, ByVal Page As Integer) As IEnumerable(Of T)
        Return dbset.OrderBy(orderby).Skip((Page - 1) * Count).Take(Count).ToList()
 End Function

Error:

 Unable to cast the type 'System.String' to type 'System.IComparable'. LINQ to Entities only supports casting Entity Data Model primitive types.

Any idea how to do this?

Extra info:

I'm in a DDD-layered application, so the parameter should stay the same as the called method is an overridden interface (eg. if i change this, i have to do this for 200 times or so, because it's in VB.Net and not in C# (= 1 change) )

I know there is a way to change the expression to a string and then use DLinq (= Dynamic Linq), but that's not how it should be.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about LINQ