拆分实体框架linq查询导致包含不起作用

时间:2016-03-30 00:51:30

标签: c# entity-framework linq

如果我这样做:

var shipmentQuery = dataAccess.Shipments().Where(x => x.OriginId == originId)
    .Take(100)
    .Include(shipment => shipment.Boxes);

return shipmentQuery;

我的盒子数据包含在结果集中。但如果我这样做:

var shipmentQuery = dataAccess.Shipments().Where(x => x.OriginId == originId);

shipmentQuery
    .Take(100)
    .Include(shipment => shipment.Boxes);

然后不包括框数据(每个结果的框为空。)

如果分解查询,包含是否应该无效?

而且,无论如何能够将其分解? (我想添加一些条件where子句。)

(我正在使用Entity Framework 6.1.3连接到sql server 2012数据库。)

1 个答案:

答案 0 :(得分:0)

将include语句与第一个语句放在一起。它实际上不会在那时查询数据库,它只是一个查询定义,因此您不必担心会被拉出额外的记录。

这样的事情:

var shipmentQuery = dataAccess.Shipments().Where(x => x.OriginId == originId)
    .Include(shipment => shipment.Boxes);

// Extra query conditions can be applied here

return shipmentQuery.Take(100);