【发布时间】:2012-12-03 05:55:24
【问题描述】:
我想将动态 lambda 表达式传递给下面的函数,但我不确定如何在表达式对象上定义 .Take() 或 .OrderByDescending()。 如果我想调用下面的函数,那么我希望能够做到这一点:
dbprovider.Query = (x => x.ConfigurationReference == "172.16.59.175")
.Take(100)
.OrderByDescending(x.Date)
FindEntities(db, dbprovider.Query)
但我不能(这种语法无效)。有什么想法吗?
public static List<T> FindEntities<T>(TrackingDataContext dataContext, System.Linq.Expressions.Expression<Func<T, bool>> find) where T : class
{
try
{
var val = dataContext.GetTable<T>().Where(find).ToList<T>();
return val;
}
catch (Exception ex)
{
throw ex;
}
}
【问题讨论】:
-
您的异常处理程序没有任何用处,顺便说一句,并且确实做了一些坏事(它丢失了堆栈跟踪);您应该删除
try/catch- 这会使整个FindEntities方法看起来非常多余