How to pass an array of OrderBy expression to a method?
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 meth开发者_运维技巧ods 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.
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...
精彩评论