简体   繁体   English

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

[英]Splitting an Entity Framework linq query causes includes to not work

If I do this: 如果我这样做:

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

return shipmentQuery;

My box data is included in the result set. 我的盒子数据包含在结果集中。 But if I do this: 但是,如果我这样做:

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

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

Then the box data is not included (boxes is null for every result.) 然后,不包括框数据(每个结果框为空。)

Are includes supposed to not work if you break up the query? 如果分解查询,包含是否应该不起作用?

And, is there anyway to be able to break it up? 而且,无论如何都可以将其分解? (I want to add a few conditional where clauses.) (我想添加一些条件where子句。)

(I am using Entity Framework 6.1.3 connecting to a sql server 2012 database.) (我正在使用连接到sql server 2012数据库的Entity Framework 6.1.3。)

Just put the include statement with the first statement. 只需将include语句和第一条语句放在一起即可。 It won't actually query the db at that point, it's just a query definition, so you don't have to worry about extra records being pulled. 那时它实际上不会查询数据库,它只是一个查询定义,因此您不必担心会拉出额外的记录。

Something like this: 像这样:

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

// Extra query conditions can be applied here

return shipmentQuery.Take(100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM