linq到xml遍历结果(linq to xml iterate through results)
我是新手使用linq特别是linq到xml,我很难尝试迭代结果。 我的xml文档有多个同名的节点嵌套在单个父节点中,我使用了以下linq查询,它返回正确的结果集。
var listingAgentElements = from p in _xElement.Descendants("commercial") select p.Elements("listingAgent");
我的xml具有以下格式:
<commercial> <listingAgent id="1"> <listingAgent id="2"> <listingAgent id="3"> </commercial>
我得到一个包含所有列表代理的结果集,虽然由于某种原因,当我尝试对listingAgentElements执行以下foreach循环时,我尝试循环的每个元素似乎具有完全相同的结果集,仍然具有所有三个listingAgents:
foreach (var element in listingAgentElements) { var test = element; }
每个listingAgent都有一组子元素,我想循环并获取存储在数据库中的值,因为我正在做一个xml导入控制台应用程序。
看来他们可能是查询的问题,但我真的不确定,有人可以帮忙吗?
I'm new to using linq in particular linq to xml and am having trouble trying to iterate through the results. My xml document has multiple nodes of the same name nested in a single parent node and I have used the following linq query which returns the correct result set.
var listingAgentElements = from p in _xElement.Descendants("commercial") select p.Elements("listingAgent");
My xml has the following format:
<commercial> <listingAgent id="1"> <listingAgent id="2"> <listingAgent id="3"> </commercial>
I am getting a result set containing all of the listing agents although for some reason when I try do the following foreach loop over listingAgentElements each element I try to loop over seems to have exactly the same result set as in still has all three listingAgents:
foreach (var element in listingAgentElements) { var test = element; }
Each listingAgent has a set of child elements in which I would like to loop over and get the values of to store in a DB as I'm doing an xml import console app.
It appears that their may be a problem with the query but I'm really not sure, could someone please assist?
原文:https://stackoverflow.com/questions/19087778
最满意答案
假设您在图表中添加了标签(现在让我们使用:Node,虽然从描述中不清楚所有节点是否应该相同,或者某些节点应该使用不同的标签),并且您有一个唯一的约束:Node(uuid) )为了快速查找,这应该工作:
MATCH (n:Node)-[:OWNER]->(o:Node) WHERE o.uuid IN $owner_ids WITH n, count(o) as cnt WHERE cnt = size((n)-[:OWNER]->()) RETURN n
您的查询在
n
和o
之间有一个笛卡尔积(图中所有节点的交叉积相互之间), 效果不佳 。 您需要在MATCH中指定关系,而不是WHERE。至于查询的其余部分,我们为每个
n
获取o
节点的数量(具有所讨论的ID的节点),并确保每个n
的数量:OWNER关系等于该计数。 如果它更大,则存在:与其他节点的OWNER关系,因此将其过滤掉。我们正在使用的
size()
函数,因为我们没有为结束节点指定任何东西,因此可以有效地获得关系计数 。Assuming you add labels to your graph (let's use :Node for now, though it's not clear from your description if all nodes should be the same or if some should use different labels), and that you have a unique constraint on :Node(uuid) for quick lookup, this should work:
MATCH (n:Node)-[:OWNER]->(o:Node) WHERE o.uuid IN $owner_ids WITH n, count(o) as cnt WHERE cnt = size((n)-[:OWNER]->()) RETURN n
Your query had a cartesian product between
n
ando
(the cross product of all the nodes of your graph with each other), which won't perform well. You need to specify the relationship in the MATCH, not the WHERE.As for the rest of the query, we're getting, for each
n
, the count ofo
nodes (those with the ids in question), and ensuring that the number of :OWNER relationships for eachn
is equal to that count. If it's greater, then there are :OWNER relationships to other nodes, so those are filtered out.The
size()
function we're using, since we aren't specifying anything for the end node, is efficient at getting relationship counts.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在python中,你可以使用内部函数来收集叶节点,然后返回它们的列表。 def get_leaf_nodes(self): leafs = [] def _get_leaf_nodes( node): if node is not None: if len(node.children) == 0: leafs.append(node) for n in node.children: ...
-
假设您在图表中添加了标签(现在让我们使用:Node,虽然从描述中不清楚所有节点是否应该相同,或者某些节点应该使用不同的标签),并且您有一个唯一的约束:Node(uuid) )为了快速查找,这应该工作: MATCH (n:Node)-[:OWNER]->(o:Node) WHERE o.uuid IN $owner_ids WITH n, count(o) as cnt WHERE cnt = size((n)-[:OWNER]->()) RETURN n 您的查询在n和o之间有一个笛卡尔积(图中所有节点的 ...
-
主要问题是,如果filter(x in rs where x.date=date)[0]确实是一个节点,则查询解析器现在不能,因此语法中不允许这样做。 幸运的是,有可能以一些冗长的代价来解决这个问题: 使用另一个WITH子句向节点变量引入别名( n )。 这将允许您使用WHERE
语法。 此外,您不能在WHERE子句中引入新变量,因此只需使用(:D)代替(d:D) 。 所以查询看起来像这样(显然,我还没有测试过): MATCH (n:A {tag_no:"N2203"})<-[:rel_a ... -
您将需要Node Relationships模块: http : //drupal.org/project/noderelationships 。 这将为您提供所需的后台关系。 Your are going to want the Node Relationships module: http://drupal.org/project/noderelationships. This will give you the back relationships that you want.
-
我如何指定这2个节点之间是否存在任何关系,而不管它们是什么关系? 我相信一个简单的MATCH就足够了。 以下查询返回名为“A”的节点与名为“B”的节点(如果存在)之间的所有关系。 MATCH ({name : "A"})-[r]->({name : "B"}) RETURN r 如果这两个节点之间存在任何关系,是否可以知道它是什么关系? type()函数返回关系类型的字符串表示形式。 然后下面的查询将返回表示A和B之间每个关系类型的字符串。 MATCH ({name : "A"})-[r]->({name ...
-
从路径获取节点(Getting nodes from paths)[2022-07-11]
您可以从路径中展开节点集合,然后返回不同的节点。 MATCH(t:THING1 {id:"t1"})-[:AFFECTS]->(x:SOME_NODE), (t)-[:CHANGES]->(SOME_NODE) MATCH p=shortestpath((s)-[MY_RELATION*0..4]->(x)) UNWIND nodes(p) as n RETURN DISTINCT n You could just unwind the collections of nodes from the path ... -
列出所有可到达的节点(List all reachable nodes)[2023-01-14]
您connected谓词似乎很好。 让基本案例依赖于has可能更有意义,但这取决于你。 但是,你说你的谓词是“双向的”,但你没有明确地创建一个指示这个的规则。 以下代码是代码的“双向”版本: has(a, b). has(b, c). has(d, b). bi_has(X,Y) :- has(X,Y). bi_has(X,Y) :- has(Y,X). connected_nodes(A, B, 0) :- write(A). connected_nodes(A, B, N) :- N > ... -
Neo4j查询删除所有节点和关系直接或间接连接(Neo4j query to delete all nodes and relations directly or indirectly connected)[2022-06-05]
以下Cypher查询将删除以特定节点为根的整个子图。 我假装你通过测试它的xxx属性值'yyy'找到子图的根。 MATCH (root {xxx:'yyy'})-[r1*]->(x) OPTIONAL MATCH ()-[r2]->(root) FOREACH(r IN r1 | DELETE r) DELETE r2, root, x; The following Cypher query will remove an entire subgraph rooted at a specific node. ...