[英]LINQ to SQL Right Outer Join
I have a SQL Query that I'm having trouble converting to LINQ query: 我有一个SQL查询,无法转换为LINQ查询:
SELECT DISTINCT Nodes.NodeName, NodeConfig.IPAddresses, NodeConfig.InSCOM, NodeConfig.InOrion, NodeConfig.OrionCustomerName, NodeConfig.OrionApplication, NodeConfig.NodeID
FROM Tags INNER JOIN
TagToNode ON Tags.TagID = TagToNode.TagID RIGHT OUTER JOIN
NodeConfig INNER JOIN
Nodes ON NodeConfig.NodeID = Nodes.NodeID ON TagToNode.NodeID = NodeConfig.NodeID
WHERE (NodeConfig.Session = '7/3/2014 1:46:33 PM') AND (NodeConfig.InSCOM = 0)
That returns 1076 rows. 返回1076行。
I tried to write the LINQ equivalent: 我试图写等效的LINQ:
var list1 = (from t in mldb.Tags
join tn in mldb.TagToNodes on t.TagID equals tn.TagID into tagJoin
from tj in tagJoin.DefaultIfEmpty()
join nc in mldb.NodeConfigs on tj.NodeID equals nc.NodeID
join n in mldb.Nodes on nc.NodeID equals n.NodeID
where (nc.Session == @"7/3/2014 1:46:33 PM") && (nc.InSCOM == 0)
select new { Customer = nc.OrionCustomerName, DeviceName = n.NodeName, DeviceType = nc.OrionApplication, IPAddress = nc.IPAddresses, NodeID = n.NodeID }).Distinct().ToList();
That returns 183 rows. 返回183行。
I have tried converting the query to inner joins as suggested by some when I searched for solutions on this site. 当我在此站点上搜索解决方案时,我曾尝试按照某些人的建议将查询转换为内部联接。 The original query implements a SQL "RIGHT OUTER JOIN" which from what I've read left/right isn't supported in LINQ but joins can be done. 原始查询实现了一个SQL“ RIGHT OUTER JOIN”,LINQ不支持我从左/右读取的内容,但可以完成连接。
The tables that I'm pulling from have primary keys as follows: 我从中提取的表具有以下主键:
[DataServiceKey(new string[] { "NodeID", "TagID" })]
public partial class TagToNode { }
[DataServiceKey(new string[] { "NodeID" })]
public partial class Node { }
[DataServiceKey(new string[] { "TagID" })]
public partial class Tag { }
[DataServiceKey(new string[] { "ConfigID" })]
public partial class NodeConfig { }
The relationship is that Nodes have many NodeConfigs, and many Nodes are Tagged with many tags. 关系是节点具有许多NodeConfig,并且许多节点带有许多标签。
Can someone help me with the query logic? 有人可以帮我查询逻辑吗?
OK so I took a step back and restructured the query. 好的,所以我退后一步,重新组织了查询。 I created an inner join first, and then left-joined that to the main query. 我先创建了一个内部联接,然后将其左联接到主查询。
I have a list of computers in a data base that are "tagged" in my application. 我在数据库中有一个在应用程序中被“标记”的计算机列表。 I wanted to be able to search the tag names in the database via the many-to-many relationship between a tag and a device, where I have a join table in the middle named "TagToNode". 我希望能够通过标签和设备之间的多对多关系来搜索数据库中的标签名称,其中中间有一个名为“ TagToNode”的联接表。
The distinct selection just weeds out the dupes in the end, the idea is to get ALL of the computers (nodes) even if they're not tagged with anything. 唯一的选择只是最后清除了重复对象,其想法是获得所有计算机(节点),即使它们没有被任何标签。
LINQ LINQ
var tags = (from tn in mldb.TagToNodes
join t in mldb.Tags on tn.TagID equals t.TagID
select new { tn.TagID, tn.NodeID, t.TagName, t.AssocUser });
return (from nc in mldb.NodeConfigs
join n in mldb.Nodes on nc.NodeID equals n.NodeID
join t in tags on n.NodeID equals t.NodeID into nj
from tg in nj.DefaultIfEmpty()
where nc.Session == sc.SessionName && n.NodeActive == 1 && ((tg.TagName.Contains(sc.SearchTerm) && (tg.AssocUser.Contains(windowsId) || (tg.AssocUser == null || tg.AssocUser == ""))) || (n.NodeName.Contains(sc.SearchTerm)) || (nc.OrionCustomerName.Contains(sc.SearchTerm)) || (nc.IPAddresses.Contains(sc.SearchTerm)))
select new NodeInfo { Customer = nc.OrionCustomerName, DeviceName = n.NodeName, DeviceType = nc.OrionApplication, IPAddress = nc.IPAddresses, NodeID = n.NodeID }).Distinct().ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.