我有一个选择查询,它反复使用不同的 where
过滤器:
var query = from row in context.Table select row;
如何将它保存到一个静态类变量中,以便我可以在不同的方法中重用它?喜欢:
var results1 = query.Where(condition1);
...
var results2 = query.Where(condition2);
最佳答案
你走在正确的轨道上。
考虑创建一个新方法而不是变量:
public IQueryable<Cust> ListActiveCustomers()
{
return dataContext.Customers.Where(c=>c.IsActive==true);
}
然后您可以从该方法可见的任何地方使用它:
//Active Customers with no invoices due.
var activePaidCustomers = ListActiveCustomers().Where(c=>c.InvoicesDue==0)
.OrderByDescending(c=>c.LastPaidOn)
.ToList();
关于c# - 创建可重用的 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3522460/