LINQ 排序:无法按类型“System.IComparable”排序

发布于 2024-10-02 10:04:57 字数 925 浏览 2 评论 0 原文

我正在构建一个小型搜索类,它使用 predicatebuilder 来获取一些结果:

这是我的查询:

var results = (from u in db.users
               join p in db.profiles on u.userId equals p.UserID
               select new SearchResult { Profile = p, User = u }).AsQueryable().Where(predicate);

结果成为 SearchResult 的可枚举:

public class SearchResult 
{
    public user User { get; set; }
    public profile Profile { get; set; }
}

这工作正常,但现在我也想对其进行排序:

var sortedResult = results.OrderBy(x => x.User.timeAdded);

这也工作得很好,除非我这样做:

Expression<Func<SearchResult, IComparable>> OrderByExpression = x => x.User.timeAdded;
var sortedResult = results.OrderBy(OrderByExpression);

我收到此错误:无法按类型“System.IComparable”订购。

这不是与将 lambda 查询直接放入 orderby 子句(有效)完全相同吗?我这样做的原因是我想将 orderby 表达式传递给另一个函数。

有人知道我做错了什么吗?谢谢!

I'm building a small search class that uses predicatebuilder to get some results:

here is my query:

var results = (from u in db.users
               join p in db.profiles on u.userId equals p.UserID
               select new SearchResult { Profile = p, User = u }).AsQueryable().Where(predicate);

Results become an enumerable of SearchResult:

public class SearchResult 
{
    public user User { get; set; }
    public profile Profile { get; set; }
}

This works fine but now I also want to sort it:

var sortedResult = results.OrderBy(x => x.User.timeAdded);

And this works fine too except when I do:

Expression<Func<SearchResult, IComparable>> OrderByExpression = x => x.User.timeAdded;
var sortedResult = results.OrderBy(OrderByExpression);

I get this error: Cannot order by type 'System.IComparable'.

Isn't this exactly the same as putting the lambda query right into the orderby clause (which works) ? The reason why I doing this is that I want to pass the orderby expression to another function.

Anyone knows what I'm doing wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心 2024-10-09 10:04:57

OrderBy 需要一个 Expression> 类型的表达式。在您的代码中,TSourceSearchResultTResultIComparable,无法转换为数据库查询。您希望 TResult 成为一个原始类型,等于 timeAdded 的具体类型(根据名称,我假设它是一个 DateTime?)这个工作。

Expression<Func<SearchResult, DateTime>> OrderByExpression = x => x.User.timeAdded;
var sortedResult = results.OrderBy(OrderByExpression);

OrderBy expects an expression of type Expression<Func<TSource, TResult>>. In your code, TSource is SearchResult and TResult is IComparable, which can't be translated to a DB query. You want TResult to be a primitive type equals to the concrete type of timeAdded, (given the name, I assume it's a DateTime?) for this to work.

Expression<Func<SearchResult, DateTime>> OrderByExpression = x => x.User.timeAdded;
var sortedResult = results.OrderBy(OrderByExpression);
北风几吹夏 2024-10-09 10:04:57

你有没有尝试过(现在无法访问VS,所以这更多是猜测):

Func<SearchResult, IComparable> OrderByFunc = x => x.User.timeAdded;
var sortedResult = results.OrderBy(OrderByFunc);

Have you tried (no access to VS right now, so it's more of a guess):

Func<SearchResult, IComparable> OrderByFunc = x => x.User.timeAdded;
var sortedResult = results.OrderBy(OrderByFunc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文