简体   繁体   中英

Outer join in linq and get records based on a condition from second table

Tables : 
Component        ComponentRights
--------------        ----------------  
ComponentId (PK)      ComponentRightsID
ComponentName         ComponentId (FK)
                      RoleId 

Lets say MyRoleId =2;

Now, I want to get all the records from both the tables but based on one condition. If I get same record but with different RoleId, then it should take only one record where RoleId = MyRoleId. If no duplicates, then condition is skipped.

Example:
Record 1:
------------
ComponentId = 1,
ComponentName = 'SampleComponent'
RoleId = 1

Record 2 :
-----------
ComponentId = 1,
ComponentName = 'SampleComponent'
RoleId = 2

* So In this case I should get Record 2.

Here is my sample code:



var Components = (from components in MyDB.Component
                                  join componentRights in MyDB.ComponentRights on components.ComponentId equals componentRights.ComponentId
                                  into AllComponents
                                  from allComponent in AllComponents.DefaultIfEmpty()

                                  where !(components.IsDeleted)

                                  select new ComponentRightsModel()
                                  {

                                      ComponentRightsId = (!allComponent.IsDeleted) ? (Guid?)allComponent.ComponentRightsId : null,
                                      ComponentId = components.ComponentUID,
                                      ComponentName = components.ComponentName,
                                      RoleId = allComponent.RoleId
                                  }).ToList();

I'd suggest decomposing the problem into separate statements...

var Components = (from components in MyDB.Component
                  join componentRights in MyDB.ComponentRights on components.ComponentId equals componentRights.ComponentId
                                  into AllComponents
                                  from allComponent in AllComponents.DefaultIfEmpty()

                              where !(components.IsDeleted)
                              select new ComponentRightsModel()
                              {
                                  ComponentRightsId = (!allComponent.IsDeleted) ? (Guid?)allComponent.ComponentRightsId : null,
                                  ComponentId = components.ComponentUID,
                                  ComponentName = components.ComponentName,
                                  RoleId = allComponent.RoleId
                              });
var uniqueComponetIds = Components.GroupBy(x=>x.ComponentId).Select(x=> new {ComponentId = x.Key, Count = x.Count(y=>y.RoleId)}).Where(x=>x.Count == 1).Select(x=>x.ComponentId);

var filteredDuplicates = Components.Where(x=> !uniqueComponetIds.Contains(x.ComponentId) && x.RoleId == MyRoleId);

var finalComponents = Components.Where(x=> uniqueComponetIds.Contains(x.ComponentId).Concat(filteredDuplicates);

Note that this query would also filter out records where the component is duplicated but where the duplicated collected doesn't happen to have a role with RoleId == MyRoleId. If you want to include these as well, you can add a third statement to capture these as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM