【发布时间】:2015-01-16 08:38:51
【问题描述】:
我有一个网页,用户可以通过单击一组 DropDownList 来指定他们的查询。现在我想根据用户的输入构建我的 sql 查询。我使用 System.Linq.Expressions 来做到这一点。
public static IEnumerable<T> FilterTable<T>(List<Filter> filters, Table<T> table) where T : class
{
int top;
IEnumerable<T> query;
if (filters == null || filters.Count == 0)
{
query = table;
}
else
{
Func<T, bool> lamda = Build<T>(filters, out top);
if (top > 0)
{
query = table.Where(lamda).Take(top);
}
else
{
query = table.Where(lamda);
}
}
return query;
}
这种方法确实有效。但它很慢,因为 IIS 首先从数据库服务器获取所有数据,然后应用 where 子句。所以在 IIS 服务器和 db 服务器之间可能会有很多不必要的开销。
那么,有没有更好的方法来做到这一点? linq to sql 中是否有与 System.Linq.Expressions 等价的东西?
【问题讨论】: