I have an existing LINQ query that needs two right joins on it, I know how I would write it in SQL but im not sure about LINQ.
This is not a question on how to do one join in linq. I need the two.
consider the following pseudo SQL query:
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
)
I want to get records in the Project Item table, where there is a record in either the UserCostingItem
table OR the UserCostingItemType
table.
Currently I'm joining on one table, but I need to right join on both and return only the records that exist in one of the joins.
here is the code I have so far which is only inner joining on one table:
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