Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a SQL "Where Exists" in LINQ to Entities?

I really want to do something like this:

Select *  from A join B on A.key = B.key join C on B.key = C.key -- propagated keys where exists (select null from B where A.key = B.key and B.Name = "Joe") and       exists (select null from C where B.key = C.key and C.Name = "Kim") 

What would the linq statement look like using Entity Framework 4 and C#?

Update:

Apparently .Contains() will produce "Where Exists" results. So, another attempt
(I don't know if this will even compile LOL):

var inner1 = from recordB in B              where recordB.Name = "Joe"              select recordB.key;  var inner2 = from recordC in C              where recordC.Name = "Kim"              select recordC.key;  var result = from recordA in A              where inner1.Contains( recordA.key) &&                    inner2.Contains( recordA.key)              select recordA; 

EDIT: WOW this is what actually worked:

var result = from A in Products              where A.kfield1 == 1 && A.kfield2 == 2 &&                    ( from B in Btable                      where B.otherid == "Joe" &&  // field I want to select by                            B.kfield1 == A.kfield1 &&                                 B.kfield2 == A.kfield2 // Can keep adding keys here                      select A.identifier  // unique identity field                     ).Contains(A.identifier) &&                    ( from C in Ctable                      where C.otherid == "Kim" &&  // field I want to select by                            C.kfield1 == A.kfield1 &&                                 C.kfield2 == A.kfield2 // Can keep adding keys here                      select A.identifier  // unique identity field                     ).Contains(A.identifier)              select A; 

This produced this SQL:

SELECT [t0].[identifier], [t0].* FROM [A] AS [t0] WHERE ([t0].[kfield1] = @p0) AND ([t0].[kfield2] = @p1) AND (EXISTS(     SELECT NULL AS [EMPTY]     FROM [B] AS [t1]     WHERE ([t0].[identifier] = [t0].[identifier]) AND ([t1].[otherid] = @p2) AND           ([t1].[kfield1] = [t0].[kfield1]) AND            ([t1].[kfield2] = [t0].[kfield2]))) AND (EXISTS(     SELECT NULL AS [EMPTY]     FROM [C] AS [t2]     WHERE ([t0].[identifier] = [t0].[identifier]) AND ([t2].[otherid] = @p3) AND           ([t2].[kfield1] = [t0].[kfield1]) AND            ([t2].[kfiekd2] = [t0].[kfield2]) )) 

Which is what I wanted. Notice the [t0].[identifier] = [t0].[identifier], which filters out null values because null doesn't compare equal to anything including itself (in SQL)

like image 787
Zachary Scott Avatar asked Nov 01 '10 23:11

Zachary Scott


People also ask

Can we use multiple where clause in LINQ?

Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.

How can we handle concurrency in LINQ?

LINQ gives three ways by which we can handle concurrency conflicts. To handle concurrency conflicts, we need to wrap the LINQ to SQL code in a TRY block and catch the ChangeConflictException . We can then loop through the ChangeConflicts collection to specify how we want the conflict to be resolved.

Which entities can LINQ use to perform queries?

LINQ to Entities queries are comprised of LINQ standard query operators (such as Select, Where, and GroupBy) and expressions (x > 10, Contact. LastName, and so on). LINQ operators are not defined by a class, but rather are methods on a class.


1 Answers

The .Any() extension method typically maps to exists.

like image 58
David Pfeffer Avatar answered Sep 30 '22 19:09

David Pfeffer