2016-11-17 98 views
1

我需要從Orders表中提取數據,這些數據未分配並且分配的訂單位於不同的表Orders_Assigned中。以下是我的Linq to Entity Code。Linq to Entity問題的左連接


問題:我沒有得到公正的未分配的訂單,而它得到雙方未分配和分配的訂單。在linq代碼下面有問題,包括和子句{和[範圍2]。[CHEM_ID] IS NULL}。有關如何在linq中將正確的sql獲取到實體代碼的任何建議。

var query = from objOrder in context.ORDERS 
          join objOrderAssigned in context.ORDERS_ASSIGNED 
          on new { key1 = objOrder.CHEM_ID, key2 = objOrder.ORDER_NBR } equals new { key1 = objOrderAssigned.CHEM_ID, key2 = objOrderAssigned.ORDER_NO } into temptbl 
          from temp in temptbl.DefaultIfEmpty() 
          where objOrder.ORDER_STATUS == "NEW" 
          select new Order 
          { 
           compoundid = temp.CHEM_ID, 
           orderno = objOrder.ORDER_NBR, 
           route = objOrder.ROUTE_CD, 
          }; 

當我看着到SQL資料,我看到一個where子句中失蹤,不知道我是缺少在這裏通過SQL配置文件生成上述LINQ到實體代碼

SQL :

SELECT 
[Project1].[CHEM_ID] AS [CHEM_ID], 
[Project1].[ORDER_NBR] AS [ORDER_NBR], 
[Project1].[ROUTE_CD] AS [ROUTE_CD], 
[Project1].[C1] AS [C1] 
FROM (SELECT 
    [Extent1].[CHEM_ID] AS [CHEM_ID], 
    [Extent1].[ORDER_NBR] AS [ORDER_NBR], 
    [Extent1].[ROUTE_CD] AS [ROUTE_CD], 
    N'' AS [C1] 
    FROM [dbo].[ORDERS] AS [Extent1] 
    LEFT OUTER JOIN [dbo].[ORDERS_ASSIGNED] AS [Extent2] ON ([Extent1].[CHEM_ID] = [Extent2].[CHEM_ID]) AND ([Extent1].[ORDER_NBR] = [Extent2].[ORDER_NO]) 
    WHERE 'NEW' = [Extent1].[ORDER_STATUS] 
) AS [Project1] 

SQL產生正確的數據

SELECT 
[Project1].[CHEM_ID] AS [CHEM_ID], 
[Project1].[ORDER_NBR] AS [ORDER_NBR], 
[Project1].[ROUTE_CD] AS [ROUTE_CD], 
[Project1].[C1] AS [C1] 
FROM (SELECT 
    [Extent1].[CHEM_ID] AS [CHEM_ID], 
    [Extent1].[ORDER_NBR] AS [ORDER_NBR], 
    [Extent1].[ROUTE_CD] AS [ROUTE_CD], 
    N'' AS [C1] 
    FROM [dbo].[ORDERS] AS [Extent1] 
    LEFT OUTER JOIN [dbo].[ORDERS_ASSIGNED] AS [Extent2] ON ([Extent1].[CHEM_ID] = [Extent2].[CHEM_ID]) AND ([Extent1].[ORDER_NBR] = [Extent2].[ORDER_NO]) 
    WHERE 'NEW' = [Extent1].[ORDER_STATUS] 
    and [Extent2].[CHEM_ID] IS NULL 
) AS [Project1] 
+0

我相信從左至右outered臨時投放時,你應該在你查詢的選擇部分檢查空值。像(temp!= null)這樣的東西? temp.CHEM_ID:null。 – JanneP

+0

@JanneP CHEM_ID是一個整數而非空列。我不知道如何在Select中添加上面的行,因爲它會拋出一個錯誤,指示它爲空。我需要在where語句中包含AND子句。 – user6747972

回答

1

下面的linq代碼有問題,包括和子句{和[範圍2]。[CHEM_ID] IS NULL}。

您忘記了包含null檢查LINQ查詢where子句。這裏唯一具體的是你不能檢查temp.CHEM_ID屬性,因爲從C#編譯器的角度來看它不可空,所以你必須在temp對象上應用檢查(EF足夠聰明,可以將它翻譯成正確的SQL)。

這裏是工作的查詢:

var query = 
    from objOrder in context.ORDERS 
    join objOrderAssigned in context.ORDERS_ASSIGNED 
    on new { key1 = objOrder.CHEM_ID, key2 = objOrder.ORDER_NBR } 
    equals new { key1 = objOrderAssigned.CHEM_ID, key2 = objOrderAssigned.ORDER_NO } 
    into assignedOrders 
    from objOrderAssigned in assignedOrders.DefaultIfEmpty() 
    where objOrder.ORDER_STATUS == "NEW" && objOrderAssigned == null 
    select new Order 
    { 
     compoundid = objOrder.CHEM_ID, 
     orderno = objOrder.ORDER_NBR, 
     route = objOrder.ROUTE_CD, 
    }; 
+0

謝謝你的搖滾。我剛剛從objOrderAssigned == null更改爲objOrderAssigned.CHEM_ID == null – user6747972