如何在LINQ to Entities中执行SQL“Where Where”?

时间:2010-11-01 23:42:04

标签: c# linq linq-to-entities

我真的想做这样的事情:

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")

使用Entity Framework 4和C#,linq语句会是什么样子?

更新

显然.Contains()会产生“Where Exists”结果。所以,另一次尝试(我不知道这是否会编译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;

编辑:哇这是实际工作的:

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;

这产生了这个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]) ))

这就是我想要的。请注意[t0]。[identifier] = [t0]。[identifier],它会过滤掉空值,因为null不会比较包含它自身的任何内容(在SQL中)

2 个答案:

答案 0 :(得分:34)

.Any()扩展程序通常会映射到exists

答案 1 :(得分:0)

您是否尝试将exists条件添加到联接中?

from a in context.AEntity
Join B in context.BEntity on A.Key equals B.Key && B.Name == "Joe"
Join C in context.CEntity on B.Key equals C.Key && C.Name == "Kim";

不确定这是否有效,但值得一试。