使用Lambda Expression进行多次连接

时间:2015-08-30 00:15:52

标签: sql linq lambda inner-join

我有三张桌子。

*Contract*
ContractID(PK)
AgentID(FK)
CustomerID(FK)
Status

*Agent*
AgentID
Name

*Customer*
CustomerID
Name

我需要编写一个查询来选择所有合同,其中代理的名称包含' abc'客户的名字包含' xyz'。

我的Linq查询是:

from c in ctx.Contracts
from a in ctx.Agents
from cu in ctx.Customers
where c.CustomerID == cu.CustomerID && c.AgentID == a.AgentID && 
a.Name.Contains("abc") && cu.Name.Contains("xyz")
select c

生成的SQL是:

SELECT 
[Filter1].[ContractID] AS [ContractID], 
[Filter1].[AgentID1] AS [AgentID], 
[Filter1].[Status] AS [Status], 
[Filter1].[CustomerID] AS [CustomerID] 
FROM   (SELECT 
        [Extent1].[ContractID] AS [ContractID], 
        [Extent1].[AgentID] AS [AgentID1], 
        [Extent1].[Status] AS [Status], 
        [Extent1].[CustomerID] AS [CustomerID] 
        FROM  [dbo].[Contract] AS [Extent1]
        INNER JOIN [dbo].[Agent] AS [Extent2] ON [Extent1].[AgentID] = 
        [Extent2].[AgentID]
        WHERE [Extent2].[FirstName] LIKE N'%abc%' ) AS [Filter1]
        INNER JOIN [dbo].[Customer] AS [Extent3] ON [Filter1].[CustomerID] =
        [Extent3].[CustomerID]
        WHERE [Extent3].[FirstName] LIKE N'%xyz%'

我正在努力在Lambda Expression中编写它。任何帮助将不胜感激。

还有我如何使用' OR'编写相同的查询。

from c in ctx.Contracts
from a in ctx.Agents
from cu in ctx.Customers
where c.CustomerID == cu.CustomerID && c.AgentID == a.AgentID && 
a.Name.Contains("abc") || cu.Name.Contains("xyz")
select c

2 个答案:

答案 0 :(得分:0)

AND版本查询的等效lambda表达式应如下所示..

var result = ctx.Contracts.Join(ctx.Agents.Where(x => x.Name.Contains("abc")), 
                                c => c.AgentID, 
                                a => a.AgentID, 
                                (c, a) => c)                  
                            .Join(ctx.Customers.Where(x => x.Name.Contains("xyz")),
                                  c => c.CustomerID, 
                                 cu => cu.CustomerID, 
                               (c, cu) => c).ToList();

这是您的OR版本

var orResult = ctx.Contracts.Join(ctx.Agents,
            c => c.AgentID,
            a => a.AgentID,
            (c, a) => new {dContract = c, agetnName = a.Name})
            .Join(ctx.Customers,
                c => c.dContract.CustomerID,
                cu => cu.CustomerID,
                (c, cu) => new {ducontract = c, customerName = cu.Name})
        .Where(x => x.ducontract.agetnName.Contains("abc") || x.customerName.Contains("xyz"))
        .Select(y => y.ducontract.dContract).ToList();

答案 1 :(得分:0)

使用join:

from c in ctx.Contracts
join a in ctx.Agents on c.AgentID equals a.AgentID
join cu in ctx.Customers on c.CustomerID equals cu.CustomerID
where a.Name.Contains("abc") && cu.Name.Contains("xyz")
select c;

RE

  

此外,我如何使用' OR'。

编写相同的查询      

来自c in ctx.Contracts from a ctx.Agents from cu in ctx.Customers   其中c.CustomerID == cu.CustomerID&& c.AgentID == a.AgentID&&   a.Name.Contains(" abc")|| cu.Name.Contains(" xyz")选择c

以上并不适用于&&运算符比||更粘。要正确编写OR表达式,请使用:

from c in ctx.Contracts
from a in ctx.Agents
from cu in ctx.Customers
where 
    c.CustomerID == cu.CustomerID && c.AgentID == a.AgentID
    && ( a.Name.Contains("abc") || cu.Name.Contains("xyz") ) 
select c

您的原始查询条件,没有括号的那个..

where c.CustomerID == cu.CustomerID && c.AgentID == a.AgentID && 
   a.Name.Contains("abc") || cu.Name.Contains("xyz") 

..评估如下。 &安培;&安培;比||

更粘
where 
    (
        c.CustomerID == cu.CustomerID 
        && c.AgentID == a.AgentID 
        && a.Name.Contains("abc")
    ) 
    || cu.Name.Contains("xyz") 

最后,这就是你想要的:

from c in ctx.Contracts
join a in ctx.Agents on c.AgentID equals a.AgentID
join cu in ctx.Customers on c.CustomerID equals cu.CustomerID
where a.Name.Contains("abc") || cu.Name.Contains("xyz")
select c;

顺便问一下,如果你能使用Linq,为什么要使用lambda?或者你可以混合Linq和lambda。仅使用lambda会使查询难以阅读:http://www.ienablemuch.com/2014/03/highfalutin-code-1.html