2013-03-21 78 views
1

什麼會導致此查詢。要開始只投射c而不填充其子集合ParcelValuesLINQ to Entities查詢不投影子集合

var result = (from c in _context.Parcels.Include("ParcelValues") 
where c.STRAP == strap 
select c); 

我跑我的應用程序和上面將返回IQueryable<Parcel>類型的result對象被填充有正確的記錄屬性Request.ParcelValues。現在,出於某種原因,無論我如何嘗試使用包含,只會投射包裹字段。

生成的SQL

exec sp_executesql N'SELECT 
[Project2].[LAND_AREA] AS [LAND_AREA], 
[Project2].[STRAP] AS [STRAP], 
[Project2].[SITUS] AS [SITUS], 
[Project2].[ZIP_CODE] AS [ZIP_CODE], 
[Project2].[MAILING_ADDRESS] AS [MAILING_ADDRESS], 
[Project2].[PROPERTY_USE] AS [PROPERTY_USE], 
[Project2].[SUBDIVISION] AS [SUBDIVISION], 
[Project2].[DSCR] AS [DSCR], 
[Project2].[INCORPORATION] AS [INCORPORATION], 
[Project2].[SEC_TWP_RGE] AS [SEC_TWP_RGE], 
[Project2].[CENSUS] AS [CENSUS], 
[Project2].[WATERFRONT] AS [WATERFRONT], 
[Project2].[C1] AS [C1], 
[Project2].[ID] AS [ID], 
[Project2].[STRAP1] AS [STRAP1], 
[Project2].[YEAR] AS [YEAR], 
[Project2].[LAND] AS [LAND], 
[Project2].[IMPROVEMENT] AS [IMPROVEMENT], 
[Project2].[JUST] AS [JUST], 
[Project2].[ASSESSED] AS [ASSESSED], 
[Project2].[TAXABLE] AS [TAXABLE] 
FROM (SELECT 
    [Limit1].[STRAP] AS [STRAP], 
    [Limit1].[SITUS] AS [SITUS], 
    [Limit1].[ZIP_CODE] AS [ZIP_CODE], 
    [Limit1].[MAILING_ADDRESS] AS [MAILING_ADDRESS], 
    [Limit1].[PROPERTY_USE] AS [PROPERTY_USE], 
    [Limit1].[SUBDIVISION] AS [SUBDIVISION], 
    [Limit1].[DSCR] AS [DSCR], 
    [Limit1].[LAND_AREA] AS [LAND_AREA], 
    [Limit1].[INCORPORATION] AS [INCORPORATION], 
    [Limit1].[SEC_TWP_RGE] AS [SEC_TWP_RGE], 
    [Limit1].[CENSUS] AS [CENSUS], 
    [Limit1].[WATERFRONT] AS [WATERFRONT], 
    [Extent2].[ID] AS [ID], 
    [Extent2].[STRAP] AS [STRAP1], 
    [Extent2].[YEAR] AS [YEAR], 
    [Extent2].[LAND] AS [LAND], 
    [Extent2].[IMPROVEMENT] AS [IMPROVEMENT], 
    [Extent2].[JUST] AS [JUST], 
    [Extent2].[ASSESSED] AS [ASSESSED], 
    [Extent2].[TAXABLE] AS [TAXABLE], 
    CASE WHEN ([Extent2].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1] 
    FROM (SELECT TOP (1) 
     [Extent1].[STRAP] AS [STRAP], 
     [Extent1].[SITUS] AS [SITUS], 
     [Extent1].[ZIP_CODE] AS [ZIP_CODE], 
     [Extent1].[MAILING_ADDRESS] AS [MAILING_ADDRESS], 
     [Extent1].[PROPERTY_USE] AS [PROPERTY_USE], 
     [Extent1].[SUBDIVISION] AS [SUBDIVISION], 
     [Extent1].[DSCR] AS [DSCR], 
     [Extent1].[LAND_AREA] AS [LAND_AREA], 
     [Extent1].[INCORPORATION] AS [INCORPORATION], 
     [Extent1].[SEC_TWP_RGE] AS [SEC_TWP_RGE], 
     [Extent1].[CENSUS] AS [CENSUS], 
     [Extent1].[WATERFRONT] AS [WATERFRONT] 
     FROM [dbo].[PARCELS] AS [Extent1] 
     WHERE [Extent1].[STRAP] = @p__linq__0) AS [Limit1] 
    LEFT OUTER JOIN [dbo].[PARCEL_VALUES] AS [Extent2] ON [Limit1].[STRAP] = [Extent2].[STRAP] 
) AS [Project2] 
ORDER BY [Project2].[STRAP] ASC, [Project2].[C1] ASC',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'0001020003' 
+0

是否使用實體框架,或者您使用LINQ到SQL?他們不是一回事! :) – 2013-03-21 20:41:01

+0

對不起。我正在使用Entity Framwork。 – 2013-03-21 20:50:00

+0

查詢明確地從'PARCEL_VALUES'表中選擇字段。你是否確定表中有相關​​的值? – 2013-03-21 20:53:37

回答

0

知道你的數據

因此,它結束了,我的整個問題正在由實體框架的一個已知的設計特性/錯誤引起的(視你如何看待它)和我的數據。我用nchar(25)將表中的密鑰從varchar(16)中導入到我的表中。在填充我的目標之前,大多數表格都是在修剪密鑰。除了單獨查詢時,我忘了修整表格上似乎不會加載的按鍵。所以,事實證明,EF不能延遲加載相關的表格,其中一些在鍵中有空白(空格必須匹配)。

一旦我更新了我的腳本加載我所有的目標SANS空白。問題解決了。