2015-09-04 104 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左