渴望加载连接的属性

时间:2014-08-05 13:30:31

标签: c# .net linq entity-framework

我正在寻找一种方法来急切加载连接表的某些属性。

情况如下: 我正在创建一个类库和Web服务将包括在内。 在这个库中必须有一个方法,它将返回具有一组专用设备值的所有设备。

在这些设备值中,引用了该值(以及其他一些实体)的选项。我正在返回一个列表,因为当我向服务提供结果时,我不希望我的连接打开。

出现以下问题:

Web服务正试图获取设备值的选项,但是因为它尚未加载而得到异常,并且ef尝试延迟加载它。 我知道我需要急于加载这些引用,但这就是问题所在。

我有以下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();

我试图在群组加入之前放置我的包含:

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

我尝试将它们放在选择

之后
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")

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

您可以在toList()来电后致电.Include()强制进行评估。

像这样:

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()不会强制执行评估,只会最大限度地减少对数据库的调用次数。