将列上的 Lambda 表达式添加到 Linq .Where()

发布于 2024-10-21 09:42:09 字数 598 浏览 5 评论 0原文

我有两个表,订单和客户,订单引用客户,以及它们的映射类订单和客户。

我想编写一个接收订单和客户的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-10-28 09:42:09

这是实现这一目标的一种方法。

void ProcessOrders(Expression<Func<Order, bool>> orderCondition, 
        Expression<Func<Customer, bool>> customerCondition>>)
{
    var orders = Database.Query<Order>().Where(orderCondition);
    var customers = Database.Query<Customer>().Where(customerCondition);

    var q = from o in orders join c in customers on o.CustomerId == c.Id
            select new {o, c}
}

同样,根据 ORM,类似的方法也可能有效(我假设这里有一个延迟加载的 Customer.Orders 集合属性):

void ProcessOrders(Expression<Func<Order, bool>> orderCondition, 
        Expression<Func<Customer, bool>> customerCondition>>)
{
    var customers = Database.Query<Customer>().Where(customerCondition);

    var q = from c in customers
            let orders = c.Orders.Where(orderCondition)
            select new {c, orders}

}

任何解决方案都必须针对特定的 linq 提供程序进行测试,因为不同的 linq 提供程序可能支持也可能不支持某些查询...

This is one way to achieve it.

void ProcessOrders(Expression<Func<Order, bool>> orderCondition, 
        Expression<Func<Customer, bool>> customerCondition>>)
{
    var orders = Database.Query<Order>().Where(orderCondition);
    var customers = Database.Query<Customer>().Where(customerCondition);

    var q = from o in orders join c in customers on o.CustomerId == c.Id
            select new {o, c}
}

Again, depending on the ORM, something like this may also work (I'm assuming a lazily loaded Customer.Orders collection property here):

void ProcessOrders(Expression<Func<Order, bool>> orderCondition, 
        Expression<Func<Customer, bool>> customerCondition>>)
{
    var customers = Database.Query<Customer>().Where(customerCondition);

    var q = from c in customers
            let orders = c.Orders.Where(orderCondition)
            select new {c, orders}

}

Any solution has to be tested against the specific linq provider, since different linq providers may or may not support certain queries...

南城追梦 2024-10-28 09:42:09

你可以尝试这样的事情:

var q = (from o in Database.Query<Order>().Where(orderCondition)
         join c in Database.Query<Customer>().Where(customerCondition) on o.CustomerID equals c.CustomerID
         select new { Order = o, Customer = c });

You could try something like this:

var q = (from o in Database.Query<Order>().Where(orderCondition)
         join c in Database.Query<Customer>().Where(customerCondition) on o.CustomerID equals c.CustomerID
         select new { Order = o, Customer = c });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文