我正在尝试增强我的存储库,以便它负责订购。我已经应用了这个问题的答案,就存储库而言,我'我很确定它完成了。
我遇到的问题是我不确定现在如何将数组传递给存储库中的方法。编译器一直对我大喊关于委托的事。在上面的链接问题中,作者本质上是在做我想做的事情,所以它一定是可能的。
这是我的存储库代码:
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.
发布评论
评论(1)
然后只需将其用作
SelectOrderedList(o1 => (o1.Something), o2 => (o2.SomethingElse))
...另外,再写一个降序:)
如果你想要一个,其中每个排序者可以升序或降序,将签名替换为 Tuple>,SortDirection>;但是你不能将隐式类型的 lambda 与隐式类型的元组一起使用(并且你也不能将它们与隐式表达式一起使用),所以在使用它时你会得到一个相当丑陋的代码......
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...