1

I am trying to Order by a query using the following efcore query

Getting the PropertyInfo for the column which needs to be orderedBy using this below

var propertyInfo = typeof(TableVM).GetProperty("Type");

The EfCore Query

db.Table
 **//LinkKit Extension Method**
 .AsExpandable()
 .Where(whereClause)
 .Select(m => new TableVM
 {
    id= m.Id,
    name = m.Name,
    description = m.Description,
    type = m.Type,
    status = m.Status
})
**// Conversion of the OrderBy fails, which throws an exception saying it can't convert it into Linq query.**
.OrderBy(x => propertyInfo.GetValue(x, null))
.Skip(skip)
.Take(take)
.ToList();

C# - code to order by a property using the property name as a string

This works in EfCore 2.

Tested with removing the LinqKit extension in EfCore 3.1.3, it throws error only for OrderBy stating it can't convert it into Linq

Am I doing any mistake in the EfCore Query? Thanks in advance

DavidG
  • 95,392
  • 10
  • 185
  • 181
armourshield
  • 169
  • 1
  • 12
  • 2
    EF doesn't know how to translate that code into an SQL query, you can't use reflection like this I'm afraid. EF Core 2 would have given you warnings about this in the logs and then done everything in memory rather than in the database. – DavidG May 06 '20 at 12:56
  • 2
    Don't use the accepted answer from the link, because it doesn't work for `IQueryable`. Instead, use some of the `IQueryable` / expression based answers. – Ivan Stoev May 06 '20 at 13:00
  • Does this answer your question? [C# - code to order by a property using the property name as a string](https://stackoverflow.com/questions/1689199/c-sharp-code-to-order-by-a-property-using-the-property-name-as-a-string) – DavidG May 06 '20 at 13:02
  • I am still learning efcore, but we get the entity in the memory after we do things like .ToList() or .AsEnumerable() right? So EfCore 2 was converting this query all along to a Linq and executing on the Db right. The issue I am facing is I need to OrderBy to get things in Alphabetical order. Wondering that is possible? – armourshield May 06 '20 at 13:02
  • @DavidG I have use the first answer the Reflection one for my query. It is working in EfCore 2 but not in EfCore 3.1.3 – armourshield May 06 '20 at 13:04
  • 1
    Like Ivan said, don't use that one, use the other answers. – DavidG May 06 '20 at 13:05
  • 1
    EF Core 2 was NOT converting such conditions to SQL, but was executing the, in memory after retrieving the unfiltered data from the database. The so called client evaluation feature, which was giving you wrong impression at the same time affecting negatively performance, so it has been removed in EF Core 3.0+. That's why now you have to use translatable constructs or explicitly switch to `IEnumerable` methods. And `IQueryable` / expression based solutions are translatable. – Ivan Stoev May 06 '20 at 13:20
  • 1
    If it would be easier for you, take the code from my answer [How to use a string to create a EF order by expression?](https://stackoverflow.com/questions/39908403/how-to-use-a-string-to-create-a-ef-order-by-expression/39916384#39916384). – Ivan Stoev May 06 '20 at 13:20
  • @IvanStoev @DavidG IQueryable worked! Thanks. But just to understand EfCore well `private static Expression> ToLambda(string propertyName) ` Reflections is not a good choice for Linq Conversions. It is safe to say EfCore 2 was throwing warning and in EfCore 3.1.3 it openly rejects it? – armourshield May 06 '20 at 13:25

1 Answers1

0

There is a typo.

Your TableVM class has type property and your Entity has Type property.

So, renaming the property in TableVM class should work.

Zunayed Shahriar
  • 2,127
  • 1
  • 8
  • 11