Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列上的Lambda表达式添加到Linq.Where()中_Linq_Iqueryable - Fatal编程技术网

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

将列上的Lambda表达式添加到Linq.Where()中,linq,iqueryable,Linq,Iqueryable,我有两个表,Orders和Customers,其中Orders引用Customers,以及它们的映射类Order和Customer 我想编写一个方法,用于接收订单和客户的Linq Where()条件: void ProcessOrders(Expression<Func<Order, bool>> orderCondition, Expression<Func<Customer, bool>> customerCondition)

我有两个表,Orders和Customers,其中Orders引用Customers,以及它们的映射类Order和Customer

我想编写一个方法,用于接收订单和客户的Linq Where()条件:

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 ...
}
void ProcessOrders(表达式orderCondition,
表达式(自定义条件)
{
var q=Database.Query().Where(orderCondition);
q=q.Where(o=>o.Customer应用客户条件);//如何应用?
…q中的过程记录。。。
}
即使在阅读了大量有关此主题的SO答案和MSDN的相关文章后,我似乎仍不知道如何在Order.customer属性上应用客户条件


(正在使用的DB/ORM是Oracle/DevXPress,但我对通用解决方案没有意见,因此我没有将它们包括在标记中。请说明您的答案是否仅限于特定的DB或ORM)

这是实现此目的的一种方法

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}
}
void ProcessOrders(表达式orderCondition,
表达式customerCondition>>)
{
var orders=Database.Query().Where(orderCondition);
var customers=Database.Query().Where(customerCondition);
var q=从订单中的o加入o中的c客户。CustomerId==c.Id
选择新的{o,c}
}
同样,根据ORM的不同,类似的操作也可能会起作用(我假设这里有一个延迟加载的Customer.Orders集合属性):

void ProcessOrders(表达式orderCondition,
表达式customerCondition>>)
{
var customers=Database.Query().Where(customerCondition);
var q=客户中的c
let orders=c.orders.Where(orderCondition)
选择新{c,订单}
}

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

您可以尝试以下方法:

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 });
var q=(来自Database.Query()中的o,其中(orderCondition)
在Database.Query()中加入c,其中o.CustomerID上的(customerCondition)等于c.CustomerID
选择new{Order=o,Customer=c});

感谢您提出加入,Linq似乎偶尔会影响我的数据库知识。但是,DevExpress不支持联接,因此我的解决方案由customer.Where()查询组成,并且使用该查询的.Contains()限制订单。难看的SQL,但它似乎有效。
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 });