Linq2SQl渴望通过多个DataLoadOptions加载 I'm trying to make my Linq-to-SQL query more efficient by including child properties in one trip to the DB. I started by trying various linq queries to accomplish this. The queries were getting complex, so I tried the LoadWith() option:The constructor of my DAL class sets the LoadWith() settings:public TrackerJobData(){ dataLoadOptions = new DataLoadOptions(); dataLoadOptions.LoadWith<TrackerJobRecord>(x => x.SpecBarcodeRecords); dataLoadOptions.LoadWith<TrackerJobRecord>(x => x.TrackerJobEquipmentTriggerRecords); dataLoadOptions.LoadWith<TrackerJobRecord>(x => x.EtaRecord); this.Database.LoadOptions = dataLoadOptions;}And here is the query I'm using:public TrackerJob GetItem(int trackerJobId){ TrackerJobRecord record = (from trackerJob in this.Database.TrackerJobRecords where trackerJob.TrackerJobId == trackerJobId select trackerJob).FirstOrDefault(); return record.Map();}When I debug and F10 on just the linq query (not the return), I get this output in SQL Profiler:Pardon my ignorance of SQL Profiler, but do the three highlighted lines mean there were three round trips from the client (my code) to the server? If so, why? Will SQL Server ever execute multiple sp_executesql calls in one trip?And since I thought LoadWith() would eliminate multiple calls, what am I doing incorrectly?EDITHere are the three statements within SQL Profiler:exec sp_executesql N'SELECT TOP (1) [t0].[TrackerJobId], [t0].[Name], [t0].[EtaId], [t0].[SamplingProcessorTypeId], [t0].[Description], [t0].[LastModifiedUser], [t0].[LastModifiedTime], [t0].[VersionNumber], [t0].[Active], [t0].[Archived], [t1].[EtaId] AS [EtaId2], [t1].[EtaNumber], [t1].[Title], [t1].[State], [t1].[DateInitialized], [t1].[EtaOriginatorId], [t1].[Quantity], [t1].[Ehs], [t1].[Ship], [t1].[InternalUse], [t1].[DateClosed], [t1].[ExperimentId], [t1].[Disposition], [t1].[TestType], [t1].[LastModifiedUser] AS [LastModifiedUser2], [t1].[LastModifiedTime] AS [LastModifiedTime2], [t1].[VersionNumber] AS [VersionNumber2]FROM [AutoTracker].[TrackerJob] AS [t0]INNER JOIN [Global].[Eta] AS [t1] ON [t1].[EtaId] = [t0].[EtaId]WHERE [t0].[TrackerJobId] = @p0',N'@p0 int',@p0=17exec sp_executesql N'SELECT [t0].[SpecBarcodeId], [t0].[TrackerJobId], [t0].[EquipmentId], [t0].[StartTime], [t0].[EndTime], [t0].[LastModifiedUser], [t0].[LastModifiedTime], [t0].[VersionNumber]FROM [AutoTracker].[SpecBarcode] AS [t0]WHERE [t0].[TrackerJobId] = @x1',N'@x1 int',@x1=17exec sp_executesql N'SELECT [t0].[TrackerJobId], [t0].[EquipmentId], [t0].[LastModifiedUser], [t0].[LastModifiedTime], [t0].[VersionNumber]FROM [AutoTracker].[TrackerJobEquipmentTrigger] AS [t0]WHERE [t0].[TrackerJobId] = @x1',N'@x1 int',@x1=17 解决方案 Linq-2-sql LoadWith does not support multiple 1:N relationships.http://weblogs.asp.net/zeeshanhirani/archive/2008/08/11/constraints-with-loadwith-when-loading-multiple-1-n-relationships.aspxLinq2SQl eager load with multiple DataLoadOptions 这篇关于Linq-To-SQL多次访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-11 00:32