2015-09-04 57 views
1

我有一個現有的LINQ查詢需要兩個正確的連接,我知道如何在SQL中編寫它,但我不知道LINQ。如何將此SQL查詢轉換爲Linq

這不是一個關於如何在linq中完成一個連接的問題。我需要這兩個。

考慮下面的僞SQL查詢:

SELECT  [ProjectItem].* 
FROM  [ProjectItem] 
RIGHT JOIN [UserCostingItem] 
ON   [UserCostingItem].[CostingItemID] 
=   [ProjectItem].[ProjectItemID] 
RIGHT JOIN [UserCostingItemType] 
ON   [UserCostingItemType].[CostingItemType] 
=   [ProjectItem].[ProjectItemType] 
WHERE 
(
    [UserCostingItem].[PrimaryKey] IS NOT NULL 
    OR 
    [UserCostingItemType].[PrimaryKey] IS NOT NULL 
) 

我想在工程項目表,其中有處於或者UserCostingItem表或UserCostingItemType表中記錄的記錄。

目前我正在加入一張桌子,但我需要對兩者都進行正確連接,並只返回其中一個連接中存在的記錄。

這裏是我迄今這是隻有內側連接在一個表中的代碼:

List<PCProjectItem> projectItems = new List<PCProjectItem>(); 
List<UserCostingItem> userCostingItems = new List<UserCostingItem>(); 
List<UserCostingItemType> userCostingItemTypes = new List<UserCostingItemType>(); 

projectItems = PCProjectItem.GetProjectItems(projectID, sageDatabaseID, PCIntegrationOption); 
userCostingItems = UserCostingItem.GetUserCostingItems(userID, sageDatabaseID, documentType == null ? string.Empty : documentType.Value.ToString()); 
userCostingItemTypes = UserCostingItemType.GetUserCostingItemTypes(userID, sageDatabaseID); 

//If there are no project items or allocations, return a new list now 
if (projectItems.Count == 0 || (userCostingItems.Count == 0 && userCostingItemTypes.Count == 0)) 
{ 
    return new List<PCProjectItem>(); 
} 

//Get the project Items the user has access to only 
return (from PCProjectItem projectItem in projectItems 
     join UserCostingItem userCostingItem in userCostingItems on projectItem.PCCostItemID equals userCostingItem.CostingItemID 
     select projectItem) 
     .Distinct() // Distinct Records 
     .ToList(); // Convert to list 
+0

可能重複加入並正確加入](http://stackoverflow.com/questions/4497086/linq-left-join-and-right-join) – user1666620

+0

鏈接問題的非重複。問題更多的是如何做兩個正確的連接並選擇與其中任何一個相關的記錄,而不是如何進行左/右連接。 – WraithNath

+0

首先 - LINQ到什麼程序? EF,SQL,對象?無論如何,如果你定義了你的實體和底層表映射之間的正確關係,你將不必創建任何連接。使用連接而不是關係是一種強烈的氣味,表明對象模型是錯誤的,或者LINQ和ORM被用作彷彿它們是普通的ADO.NET –

回答

0

嗯,這似乎給了我需要的信息:

List<PCProjectItem> projectItems = new List<PCProjectItem>(); 
List<UserCostingItem> userCostingItems = new List<UserCostingItem>(); 
List<UserCostingItemType> userCostingItemTypes = new List<UserCostingItemType>(); 

projectItems = PCProjectItem.GetProjectItems(projectID, sageDatabaseID, PCIntegrationOption); 
userCostingItems = UserCostingItem.GetUserCostingItems(userID, sageDatabaseID, documentType == null ? string.Empty : documentType.Value.ToString()); 
userCostingItemTypes = UserCostingItemType.GetUserCostingItemTypes(userID, sageDatabaseID); 

//If there are no project items or allocations, return a new list now 
if (projectItems.Count == 0 || (userCostingItems.Count == 0 && userCostingItemTypes.Count == 0)) 
{ 
    return new List<PCProjectItem>(); 
} 

var results = from PCProjectItem projectItem in projectItems 
       join UserCostingItem userCostingItem in userCostingItems on projectItem.PCCostItemID equals userCostingItem.CostingItemID into costingItemJoin 
       from costItemRec in costingItemJoin.DefaultIfEmpty() 
       join UserCostingItemType userCostingItemType in userCostingItemTypes on projectItem.PCCostItemTypeID equals userCostingItemType.CostingItemTypeID into costingItemTypeJoin 
       from costItemTypeRec in costingItemTypeJoin.DefaultIfEmpty() 
       where costItemTypeRec != null || costItemRec != null 
       select projectItem; 

return results.Distinct().ToList(); 
[LINQ左