如何将 OrderBy 表达式数组传递给方法?

发布于 2024-10-17 21:14:14 字数 1207 浏览 0 评论 0 原文

我正在尝试增强我的存储库,以便它负责订购。我已经应用了这个问题的答案,就存储库而言,我'我很确定它完成了。

我遇到的问题是我不确定现在如何将数组传递给存储库中的方法。编译器一直对我大喊关于委托的事。在上面的链接问题中,作者本质上是在做我想做的事情,所以它一定是可能的。

这是我的存储库代码:

public virtual IList<TEntity> SelectOrderedList(
    Expression<Func<TEntity, bool>>[] Orderers,
    bool Ascending = true) {
    IOrderedQueryable<TEntity> TemporaryQueryable = null;

    foreach (Expression<Func<TEntity, bool>> Orderer in Orderers) {
        if (TemporaryQueryable == null) {
            TemporaryQueryable = (Ascending ? this.ObjectSet.OrderBy(Orderer) : this.ObjectSet.OrderByDescending(Orderer));
        } else {
            TemporaryQueryable = (Ascending ? TemporaryQueryable.ThenBy(Orderer) : TemporaryQueryable.ThenByDescending(Orderer));
        };
    };

    return TemporaryQueryable.ToList();
}

顺便说一句,我不能 100% 确定我应该使用 Expression>。出于某种原因,我有一种感觉,它应该是 Expression>,但我不太确定。

不管怎样,如果有人能告诉我如何实际调用它,我将非常感激。如果你能让它像 params 参数一样工作,那就加分了。

I'm trying to enhance my repository so it is the one responsible for ordering. I've applied the answer from this question and as far as the repository is concerned, I'm pretty sure it done.

The problem I'm running into is that I'm not sure how to now pass an array to the methods in the repository. The compiler keeps yelling at me about delegates. In the linked question above, the author is essentially doing what I want so it must be possible.

Here's my repository code:

public virtual IList<TEntity> SelectOrderedList(
    Expression<Func<TEntity, bool>>[] Orderers,
    bool Ascending = true) {
    IOrderedQueryable<TEntity> TemporaryQueryable = null;

    foreach (Expression<Func<TEntity, bool>> Orderer in Orderers) {
        if (TemporaryQueryable == null) {
            TemporaryQueryable = (Ascending ? this.ObjectSet.OrderBy(Orderer) : this.ObjectSet.OrderByDescending(Orderer));
        } else {
            TemporaryQueryable = (Ascending ? TemporaryQueryable.ThenBy(Orderer) : TemporaryQueryable.ThenByDescending(Orderer));
        };
    };

    return TemporaryQueryable.ToList();
}

On a side note, I'm not 100% sure that I'm supposed to use Expression<Func<TEntity, bool>>. For some reason I have a feeling that it's supposed to be Expression<Func<TEntity, int>>, but I'm not too sure.

Anyway, I would really appreciate it if someone can show me how to actually call that. Bonus points and love if you can make it work like a params argument.

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

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

发布评论

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

评论(1

<逆流佳人身旁 2024-10-24 21:14:14
public virtual IList<TEntity> SelectOrderedList(
    params Expression<Func<TEntity, IComparable>>[] Orderers) {
    IOrderedQueryable<TEntity> TemporaryQueryable = null;

    foreach (Expression<Func<TEntity, IComparable>> Orderer in Orderers) {
        if (TemporaryQueryable == null) {
            TemporaryQueryable = this.ObjectSet.OrderBy(Orderer);
        } else {
            TemporaryQueryable = TemporaryQueryable.ThenBy(Orderer);
        };
    };

    return TemporaryQueryable.ToList();
}

然后只需将其用作 SelectOrderedList(o1 => (o1.Something), o2 => (o2.SomethingElse))...

另外,再写一个降序:)

如果你想要一个,其中每个排序者可以升序或降序,将签名替换为 Tuple>,SortDirection>;但是你不能将隐式类型的 lambda 与隐式类型的元组一起使用(并且你也不能将它们与隐式表达式一起使用),所以在使用它时你会得到一个相当丑陋的代码......

public virtual IList<TEntity> SelectOrderedList(
    params Expression<Func<TEntity, IComparable>>[] Orderers) {
    IOrderedQueryable<TEntity> TemporaryQueryable = null;

    foreach (Expression<Func<TEntity, IComparable>> Orderer in Orderers) {
        if (TemporaryQueryable == null) {
            TemporaryQueryable = this.ObjectSet.OrderBy(Orderer);
        } else {
            TemporaryQueryable = TemporaryQueryable.ThenBy(Orderer);
        };
    };

    return TemporaryQueryable.ToList();
}

Then just use it as SelectOrderedList(o1 => (o1.Something), o2 => (o2.SomethingElse))...

Also, write another for Descending :)

If you want one, where each orderer can be ascending or descending, replace the signature with Tuple>,SortDirection> but you cannot use implicitly typed lambdas with implicitly typed tuples (and you cannot use them with implicit expressions either) so then, you'd have quite an ugly code when using it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文