简体   繁体   English

IQueryable.OrderBy 不能与 EF Core 中的 IComparer 一起使用

[英]IQueryable.OrderBy is not working with IComparer in EF Core

We have a signature of a method in a IQueryable interface:我们在 IQueryable 接口中有一个方法的签名:

public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer);

I am trying to implement this call like我正在尝试像这样实现这个调用

var result = await DbSet.OrderBy(e => e.Type, new EntityTypeComparer()).ToListAsync();

But it throws the exception "The LINQ expression could not be translated".但它抛出异常“无法翻译 LINQ 表达式”。

Can you share any working examples of IComparer with EF Core?您能否与 EF Core 分享 IComparer 的任何工作示例? Or how I can implement custom sorting logic for int property in EF Core?或者我如何在 EF Core 中为 int 属性实现自定义排序逻辑?

PS Microsoft docs says that IComparer is not supported. PS Microsoft 文档说不支持 IComparer。 But what do we need IQueryable.OrderBy with IComparer parameter for?但是我们需要 IQueryable.OrderBy 和 IComparer 参数做什么?

That is already explained to this case here Is it possible to use IComparable to compare entities in Entity Framework?这已经在此处对这种情况进行了解释IComparable to compare entities in Entity Framework?

The short Answer is No, you cant do that.简短的回答是不,你不能那样做。

EF requires all operations to be able to execute 100% server-side. EF 要求所有操作都能够 100% 在服务器端执行。 Supporting IComparable or IEquatable would require EF to be able to translate arbitrary IL into SQL, which it can't yet do.支持 IComparable 或 IEquatable 需要 EF 能够将任意 IL 转换为 SQL,但它目前还做不到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM