使用Lambda查询集合/连接表

时间:2013-01-15 11:57:17

标签: c# lambda

我有一个lambda'where'查询,查询Order表如下:

List<Order> returnedOrders = _session.Query<Script>()Where(s => s.orderId == orderIdParam).ToList();

我还要检查相关OrderDetails表中列的值。因此returnedOrdersOrderDetails的集合,即returnedOrders.orderDetails

因此SQL查询看起来像:

Where OrderId = 12345 and Order.OrderDetail.CreatedDate = '01-Jan-2013'

有人能帮我解决正确的语法吗?

2 个答案:

答案 0 :(得分:1)

您可以在&&方法中使用逻辑AND(Where):

var date = new Date(2013,1,1);

List<Order> returnedOrders = 
    _session.Query<Script>()
            .Where(s => 
                s.orderId == orderIdParam && 
                s.OrderDetails.Any(d => d.CreatedDate == date))
            .ToList();

您也可以添加其他Where方法。

var date = new Date(2013,1,1);

List<Order> returnedOrders = 
    _session.Query<Script>()
            .Where(s => s.orderId == orderIdParam)
            .Where(s => s.OrderDetails.Any(d => d.CreatedDate == date))
            .ToList();

答案 1 :(得分:0)

既然你知道秩序的Id,它似乎没用,但是像这样

from order in orders 
from detail in order.Details 
where 
    order.Id = orderId && 
    order.Id == detail.OrderId && 
    detail.CreateDate == createDate
select order