连接属性的即时加载
本文关键字:加载 属性 连接 | 更新日期: 2023-09-27 18:16:23
我正在寻找一种方法来加载一些属性的连接表
情况如下:我正在制作一个类库和web服务将包括。在这个标准库中,必须有一个方法可以返回所有带有特定设备值集的设备。
在这些设备值中有对该值(和其他一些实体)的选项的引用。我返回一个列表,因为我不希望我的连接是开放的,当我给服务的结果。
出现以下问题:
web服务正在尝试获取设备值的选项,但得到一个异常,因为它尚未加载,并尝试延迟加载它。我知道我需要急于加载这些引用,但这就是我的问题所在。
我有以下linq表达式:
db.Devices.Where(d => (d.Online)).Include("Pictures").Include("Brand").GroupJoin(
db.DeviceValues.Where(dv => (((((((((((((
dv.OptionId == new Guid("017AE564-742D-4389-91F8-805A58C77240")) ||
(dv.OptionId == new Guid("4BDD30A6-FBD7-4FFE-A4BA-F25BE2BE4586"))) ||
(dv.OptionId == new Guid("C6063BA6-231A-424A-92F4-A64BB1BABB7D"))) ||
(dv.OptionId == new Guid("57BD5C0B-5981-48EB-AE71-A52703FCA0CF"))) ||
(dv.OptionId == new Guid("A920ED14-8BB4-4097-B3CF-2DE7C79F34DD"))) ||
(dv.OptionId == new Guid("D29C64EF-69E1-46BB-909B-9B330031A493"))) ||
(dv.OptionId == new Guid("C3357431-5F4A-40F4-8FDD-1480E2F83D38"))) ||
(dv.OptionId == new Guid("7CB17F5E-03D3-40E9-A415-3D07BB9A1693"))) ||
(dv.OptionId == new Guid("D9FCE982-DD66-4FF3-A2A9-5BC84D2BFFF1"))) ||
(dv.OptionId == new Guid("0E008C9E-A306-E411-AB50-0050569C157F"))) ||
(dv.OptionId == new Guid("57EF5C9D-CE4D-40D4-93FE-FE3A2A9A4BDA"))) ||
(dv.OptionId == new Guid("085DE743-A18D-44A6-ACDE-EA5102290F48"))) ||
(dv.OptionId == new Guid("64A6279A-AB07-41C7-8E6D-3397FBC64FF3")))
), d => d.Id, dv => dv.DeviceId, (d, deviceValues) => new DeviceModel
{
Id = d.Id,
DeviceValues = deviceValues.ToList(),
Brand = d.Brand,
BrandId = d.BrandId,
Created = d.Created,
DeviceCategorie = d.DeviceCategorie,
Ean = d.Ean,
DeviceType = d.DeviceType,
IntroductionDate = d.IntroductionDate,
Moderator = d.Moderator,
Modified = d.Modified,
Name = d.Name,
Online = d.Online,
Pictures = d.Pictures,
SubDeviceFrom = d.SubDeviceFrom,
SubDeviceFromId = d.SubDeviceFromId,
dealId = d.dealId,
display = d.display,
oldId = d.oldId
}).ToList();
我试着把我的include放在group join之前:
db.Devices.Where(d => (d.Online)).Include("Pictures").Include("Brand").Include("Option").GroupJoin
:
db.Devices.Where(d => (d.Online)).Include("Pictures").Include("Brand").Include("DeviceValues.Option").GroupJoin
我试着把它们放在select
后面new DeviceModel
{
Id = d.Id,
DeviceValues = deviceValues.ToList(),
Brand = d.Brand,
BrandId = d.BrandId,
Created = d.Created,
DeviceCategorie = d.DeviceCategorie,
Ean = d.Ean,
DeviceType = d.DeviceType,
IntroductionDate = d.IntroductionDate,
Moderator = d.Moderator,
Modified = d.Modified,
Name = d.Name,
Online = d.Online,
Pictures = d.Pictures,
SubDeviceFrom = d.SubDeviceFrom,
SubDeviceFromId = d.SubDeviceFromId,
dealId = d.dealId,
display = d.display,
oldId = d.oldId
}).Include("Option")
:
new DeviceModel
{
Id = d.Id,
DeviceValues = deviceValues.ToList(),
Brand = d.Brand,
BrandId = d.BrandId,
Created = d.Created,
DeviceCategorie = d.DeviceCategorie,
Ean = d.Ean,
DeviceType = d.DeviceType,
IntroductionDate = d.IntroductionDate,
Moderator = d.Moderator,
Modified = d.Modified,
Name = d.Name,
Online = d.Online,
Pictures = d.Pictures,
SubDeviceFrom = d.SubDeviceFrom,
SubDeviceFromId = d.SubDeviceFromId,
dealId = d.dealId,
display = d.display,
oldId = d.oldId
}).Include("DeviceValues.Option")
任何帮助都非常感谢
可以通过在.Include()
调用之后调用toList()
来强制求值。
:
db.Devices.Where(d => (d.Online)).Include("Pictures").Include("Brand").Include("Option").toList().GroupJoin
db.Devices.Where(d => (d.Online)).Include("Pictures").Include("Brand").Include("DeviceValues.Option").toList().GroupJoin
.Include()
不强制评估,它只减少调用你的数据库的数量。