如何将一个OrderBy表达式的数组传递给一个方法? [英] How to pass an array of OrderBy expression to a method?

查看:144
本文介绍了如何将一个OrderBy表达式的数组传递给一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的存储库代码:

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< Func< TEntity,bool>> 。由于某种原因,我有一种感觉,它应该是 Expression< Func< TEntity,int>> ,但我不太确定。

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.

无论如何,如果有人能告诉我如何实际调用,我真的很感激。奖金积分和爱情,如果你可以像 params 参数一样工作。

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.

推荐答案

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)) ...

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

另外,另一个为Descending :)

Also, write another for Descending :)

如果你想要一个,每个订阅者可以升序或降序,用Tuple>,SortDirection>替换签名,但是不能使用隐式类型的lambdas与隐式类型的元组(并且你也不能用隐式表达式使用它们),所以在使用它时你会有一个很丑的代码...

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...

这篇关于如何将一个OrderBy表达式的数组传递给一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆