参数类型为:
System.Linq.Expressions.Expression<Func<T, bool>> find
这意味着它可以采用谓词(“where”子句),并且只能采用谓词。因此,您可以在那里传递的唯一位是过滤器:
x => x.ConfigurationReference == "172.16.59.175"
要执行您想要的操作,您需要在 中添加其余代码FindEntities
,使其变为:
var val = dataContext.GetTable<T>().Where(find)
.OrderByDescending(x => x.Date).Take(100).ToList<T>();
(另请注意,Take
应该真的在之后OrderByDescending
)
您可以这样做的一种方法是:
public static List<T> FindEntities<T>(TrackingDataContext dataContext,
System.Linq.Expressions.Expression<Func<T, bool>> find,
Func<IQueryable<T>, IQueryable<T>> additonalProcessing = null
) where T : class
{
var query = dataContext.GetTable<T>().Where(find);
if(additonalProcessing != null) query = additonalProcessing(query);
return query.ToList<T>();
}
并致电:
var data = FindEntities(db, x => x.ConfigurationReference == "172.16.58.175",
q => q.OrderByDescending(x => x.Date).Take(100));
但是,坦率地说,我不确定这样做的意义是什么......调用者可以更方便地在本地完成所有这些操作,而无需使用FindEntities
。只是:
var data = db.GetTable<T>()
.Where(x => x.ConfigurationReference == "172.16.58.175")
.OrderByDescending(x => x.Date).Take(100).ToList();
甚至:
var data = db.SomeTable
.Where(x => x.ConfigurationReference == "172.16.58.175")
.OrderByDescending(x => x.Date).Take(100).ToList();
要不就:
var data = (from row in db.SomeTable
where row.ConfigurationReference == "172.16.58.175"
orderby row.Date descending
select row).Take(100).ToList();