将列上的 Lambda 表达式添加到 Linq .Where()
我有两个表,订单和客户,订单引用客户,以及它们的映射类订单和客户。
我想编写一个接收订单和客户的 LinqWhere() 条件的方法:
void ProcessOrders(Expression<Func<Order, bool>> orderCondition,
Expression<Func<Customer, bool>> customerCondition)
{
var q = Database.Query<Order>().Where(orderCondition);
q = q.Where(o => o.Customer APPLY customerCondition); // how ?
... process records in q ...
}
即使在阅读了有关此主题的大量 SO 答案以及 MSDN 上的相关文章后,我似乎仍无法弄清楚如何将客户条件应用于订单.客户属性。
(使用的 DB/ORM 是 Oracle/DevExpress,但我对通用解决方案很满意,所以我没有将它们包含在标签中。请注明您的答案是否仅限于特定的 DB 或 ORM)
I have two tables, Orders and Customers, with Orders referencing Customers, and their mapped classes Order and Customer.
I want to write a method which receives a Linq Where() condition for both Orders and Customers:
void ProcessOrders(Expression<Func<Order, bool>> orderCondition,
Expression<Func<Customer, bool>> customerCondition)
{
var q = Database.Query<Order>().Where(orderCondition);
q = q.Where(o => o.Customer APPLY customerCondition); // how ?
... process records in q ...
}
Even after reading lots of SO answers on this topic and related articles on MSDN, it seems I cannot figure out how to apply the customer condition on the Order.Customer property.
(DB/ORM in use is Oracle/DevExpress, but I'm fine with a generic solution, so I did not include them in the tags. Please mention if your answer is restricted to a specific DB or ORM)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是实现这一目标的一种方法。
同样,根据 ORM,类似的方法也可能有效(我假设这里有一个延迟加载的 Customer.Orders 集合属性):
任何解决方案都必须针对特定的 linq 提供程序进行测试,因为不同的 linq 提供程序可能支持也可能不支持某些查询...
This is one way to achieve it.
Again, depending on the ORM, something like this may also work (I'm assuming a lazily loaded Customer.Orders collection property here):
Any solution has to be tested against the specific linq provider, since different linq providers may or may not support certain queries...
你可以尝试这样的事情:
You could try something like this: