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))
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
Post a Comment