简体   繁体   English

需要左外连接类型时在LINQ中连接表的问题

[英]Issue with joining tables in LINQ when needing a left outer join type

I have two tables: 我有两张桌子:

Network_Clients
Clients

Network_Clients has three columns Network_Clients有三列

networkClientID: Key
Name: Varchar
Description: Varchar

Client has 4 columns: 客户有4列:

clientID: Key
networkClientId: FK to Network_Clients -> networkClientID
Name: Varchar
Description: Varchar

Clients Table: 客户表:

clientID    networkClientID name    description
1                2          Client1 Client of Client2
2                3          Client1 Client of Client3
3                3          Client4 Client of Client3
4                1          Client4 Direct Placement Client
5                1          Client1 Direct Placement Client

Network Clients Table: 网络客户端表:

networkClientID name            description
1           Direct Placer       NULL
2           Client2             Network Client Client2
3           Client3             Network Client Client3
4           Test One            Test One Network Client

Here is my LINQ query: 这是我的LINQ查询:

from cn in Clients_Network
join c in Clients on cn.networkClientID equals c.networkClientID
select new { cn, c }

This returns everything but Direct Placer and Test One because neither of those have a linked field in the Clients table. 这将返回Direct PlacerTest One因为它们都没有Clients中的链接字段。

How can I get those two to show up? 我如何才能让这两个出现?

Use group join (ie join...into): 使用组连接 (即join ... into):

from cn in Clients_Network
join c in Clients on cn.networkClientID equals c.networkClientID into g
from cc in g.DefaultIfEmpty()
select new { cn, cc }

Thats an equivalent of left join in SQL 这相当于SQL中的左连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM