2

我有以下查询:

var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
              where e.Customers.ID == customer.ID
              select e;

一切正常,我得到了我的设备,它正确(急切地)加载了制造商表。但是当我尝试执行以下多对多查询时:

var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
              where e.Customers.ID == customer.ID
              from cce in e.ContractEquipments
              where cce.Contracts.EndedOn >= DateTime.Today
              select e;

其中 "ContractEquipments" 是 "Equipments" 和 "Contracts" 之间的多对多查找,但是当此查询运行时,不再容易加载制造商表。知道如何在不执行以下操作的情况下解决此问题:

if (MyEntity.Manufacturers.IsLoaded == false) 
   MyEntity.ManufacturersReference.Load()

这个项目需要几个小时执行,我想减少数据库调用的数量。

编辑#1:

我也试过这个没有成功:

var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
              where e.Customers.ID == customer.ID
              join cce in ContractContext.ContractEquipments 
                on e.ID equals cce.Equipments.ID
              where cce.Contracts.EndedOn >= DateTime.Today
              select e;
4

2 回答 2

7

早期包含经常在某些类型的查询中丢失(即额外的连接等)

解决这个问题的方法是进行查询,(然后只要您返回实体,即选择 e 而不是投影,即选择新 {...}),您可以转换为 ObjectQuery 并在外部进行包含:

var MyQuery = ((from e in ContractContext.Equipments
              where e.Customers.ID == customer.ID
              from cce in e.ContractEquipments
              where cce.Contracts.EndedOn >= DateTime.Today
              select e) as ObjectQuery<Equipment>).Include("Manufacturers");

这应该有效。

如果您对这方面的更多信息感兴趣,请查看提示 22 - 如何使 Include 真正包含

亚历克斯

于 2009-06-02T04:09:02.310 回答
0

您是否尝试过这样的加入?

var MyQuery = from e in ContractContext.Equipments.Include("Manufacturers")
              where e.Customers.ID == customer.ID
              join cce in e.ContractEquipments on e.Id equals cce.EquipmentId
              where cce.Contracts.EndedOn >= DateTime.Today
              select e;
于 2009-06-01T20:35:40.697 回答