Linq/XML:在 XML 元素内正确分组结果 - 使用内部联接!
在 上一个问题中,我询问了如何对 XML 元素进行分组从逻辑上讲,我得到了答案,那就是嵌套 Linq 查询。
问题是,这会产生左连接嵌套查询的效果。例如,假设我想列出美国所有以字母“Y”开头的城市,按州和县分组:
XElement xml = new XElement("States",
from s in LinqUtils.GetTable<State>()
orderby s.Code
select new XElement("State",
new XAttribute("Code", s.Code),
new XAttribute("Name", s.Name),
from cy in s.Counties
orderby cy.Name
select new XElement("County",
new XAttribute("Name", cy.Name),
from c in cy.Cities
where c.Name.StartsWith("Y")
orderby c.Name
select new XElement("City",
new XAttribute("Name", c.Name)
)
)
)
);
Console.WriteLine(xml);
此输出:
<States>
<State Code="AK" Name="Alaska ">
<County Name="ALEUTIANS EAST" />
<County Name="ALEUTIANS WEST" />
<County Name="ANCHORAGE" />
<County Name="BETHEL" />
...
<County Name="YAKUTAT">
<City Name="YAKUTAT" />
</County>
<County Name="YUKON KOYUKUK" />
</State>
<State Code="AL" Name="Alabama ">
<County Name="AUTAUGA" />
...
etc.
我不想要左连接效果;我只想查看实际包含以字母“Y”开头的城市的州和县。
我可以想到几种方法来做到这一点,但它们看起来都很笨拙且不优雅。您能想到达到预期效果的最巧妙方法是什么?
In a previous question I asked about how to group XML elements logically, and I got the answer, which was to nest the Linq query.
Problem is, this has the effect of left-joining the nested queries. For example, let's say I want to list all the cities in the USA that begin with the letter "Y", grouped by State and County:
XElement xml = new XElement("States",
from s in LinqUtils.GetTable<State>()
orderby s.Code
select new XElement("State",
new XAttribute("Code", s.Code),
new XAttribute("Name", s.Name),
from cy in s.Counties
orderby cy.Name
select new XElement("County",
new XAttribute("Name", cy.Name),
from c in cy.Cities
where c.Name.StartsWith("Y")
orderby c.Name
select new XElement("City",
new XAttribute("Name", c.Name)
)
)
)
);
Console.WriteLine(xml);
This outputs:
<States>
<State Code="AK" Name="Alaska ">
<County Name="ALEUTIANS EAST" />
<County Name="ALEUTIANS WEST" />
<County Name="ANCHORAGE" />
<County Name="BETHEL" />
...
<County Name="YAKUTAT">
<City Name="YAKUTAT" />
</County>
<County Name="YUKON KOYUKUK" />
</State>
<State Code="AL" Name="Alabama ">
<County Name="AUTAUGA" />
...
etc.
I don't want the left-join effect; I only want to see the states and counties that actually contain cities beginning with the letter "Y".
I can think of a few ways to do this, but they all seem kludgy and inelegant. What is the neatest way you can think of to achieve the desired effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以解决这个问题,但没有一种方法非常优雅。一些选项:
选项 1:使用
let
捕获子查询并过滤掉空值:选项 2:使用带有 group by 的内连接方法而不是使用 unique:
There are several ways to solve this problem, but none are exceedingly elegant. A few options:
Option 1: Use
let
to capture the subqueries and filter out the empty values:Option 2: Use your inner join approach with group by instead of distinct:
我认为你有一个好的开始。您可以将有关国家/地区和州的信息添加到您的
城市
列表中,然后对它们进行分组
,从而避免第二次加入和过滤。您甚至可以在一个大型 linq 查询中执行此操作。很难准确地编写您需要的内容,因为您有自己的类,但这里有一些与文件和文件夹类似的内容(您需要添加另一个级别):
生成 XML:
同样,这里的关键是使用
对结果进行分组
。I think you have a good start. You can add information about countries and states to your
Cities
list, and thengroup by
them, and avoiding the second join and filter.You can even do this in one big linq query. It's hard to write exactly what you need because you have your own classes, but here's something similar with files and folders (you'll need to add another level):
Resulting in the XML:
Again, the key here is to use
group by
on the results.一种方法是:首先使用所有正确的内部联接创建查询,然后使用
Distinct()
过滤器创建外部分组,然后使用where< 从分组创建 XML /code> 子句来加入它们。因此:
它有效,但不知何故我感觉有更好的方法......
Here's one approach: You first create the query with all the correct inner joins, then you create the outer groupings using a
Distinct()
filter, then create the XML from the groupings using awhere
clause to join them. Thus:It works, but somehow I have the feeling that there's a better way...