Correlated SubQuery SQL To LINQ
select * from Table1 where TC in (select TC from Table2 where Application in ('AAA'))` help me in converting above query to LINQ.
Solution 1:
Without where Application in ('AAA') part this looks quite simple:
from t1 in db.Table1s
where db.Table2s.Select(t2 => t2.TC).Contains(t1.TC)
from t1 in db.Table1s
UPDATE (How wrong I was!)
List<string> myCollection = new List<string> { "AAA" };
from t1 in db.Table1s
where db.Table2s.Where(t2 => myCollection.Contains(t2.Application)).Select(t2 => t2.TC).Contains(t1.TC)
from t1 in db.Table1s
should work with in-code collections.
Solution 2:
Try in this way
There is no 'In' subquery in LINQ (so far).
Use the 'Any' operator to accomplish the same thing.
For example:
all customers that are located in the same city as an employee
from c in db.Customers
where db.Employees.Any(e => e.City == c.City)
select c;
or
The left-hand-side of the .Any() operator is the subquery.
query.Any(x => predicate)
is equivalent to the SQL
EXISTS(
SELECT *
FROM query
WHERE predicate
)
get more details here
Post a Comment for "Correlated SubQuery SQL To LINQ"