LINQ: implement IN tables with LINQ to Entity

In SQL so many times we need to filter value from one table when the values are exist on other table.
As Below

SELECT
   GroupID
,

   GroupName
,

   GroupNumber
,
 FROM
   TableA 
WHERE

   GroupID
IN (SELECT GroupID FROM TableB)
 

You can do it as below:

 var temp =context.TableA
         
.Where(x=>context.TableB.Any(y=>y.GroupID!=x.GroupID))

         
.Select(x=>new { GroupID = x.GroupID, GroupName=x.GroupName, GroupNumber = x.GroupNumber}).ToList();


or

return DomainContext.TableA.Where(a => DomainContext.TableB.Any(b => b.GroupId == a.GroupId))

Comments

Popular posts from this blog

LINQ: Using int.TryParse() within a LINQ where clause

LINQ: Implement NOT IN tables with LINQ to Entity

AWS-Python: Fetching credentials stored in AWS Secret Manager with Python