Linq/XML:在 XML 元素内正确分组结果 - 使用内部联接!

发布于 2024-08-03 04:18:37 字数 1394 浏览 2 评论 0原文

上一个问题中,我询问了如何对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2024-08-10 04:18:37

有多种方法可以解决这个问题,但没有一种方法非常优雅。一些选项:

选项 1:使用 let 捕获子查询并过滤掉空值:

XElement xml = new XElement("States",
  from s in LinqUtils.GetTable<State>()
  let counties = from cy in s.Counties
                 let cities = from c in cy.Cities
                              where c.Name.StartsWith("Y")
                              orderby c.Name
                              select new XElement("City",
                                new XAttribute("Name", c.Name)
                              )
                 where cities.Any()
                 orderby cy.Name
                 select new XElement("County",
                   new XAttribute("Name", cy.Name),
                   cities          
                 )
  where counties.Any()
  orderby s.Code 
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),
    counties
  )
);

选项 2:使用带有 group by 的内连接方法而不是使用 unique:

XElement xml = new XElement("States",
  from s in LinqUtils.GetTable<State>()
  from cy in s.Counties
  from c in cy.Cities
  where c.Name.StartsWith("Y")
  group new { cy, c } by s into gs
  let s = gs.Key
  orderby s.Code 
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),

    from g in gs
    group g.c by g.cy into gcy
    let cy = gcy.Key
    orderby cy.Name
    select new XElement("County",
      new XAttribute("Name", cy.Name),

      from c in gcy
      orderby c.Name
      select new XElement("City",
        new XAttribute("Name", c.Name)
      )
    )
  )
);

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:

XElement xml = new XElement("States",
  from s in LinqUtils.GetTable<State>()
  let counties = from cy in s.Counties
                 let cities = from c in cy.Cities
                              where c.Name.StartsWith("Y")
                              orderby c.Name
                              select new XElement("City",
                                new XAttribute("Name", c.Name)
                              )
                 where cities.Any()
                 orderby cy.Name
                 select new XElement("County",
                   new XAttribute("Name", cy.Name),
                   cities          
                 )
  where counties.Any()
  orderby s.Code 
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),
    counties
  )
);

Option 2: Use your inner join approach with group by instead of distinct:

XElement xml = new XElement("States",
  from s in LinqUtils.GetTable<State>()
  from cy in s.Counties
  from c in cy.Cities
  where c.Name.StartsWith("Y")
  group new { cy, c } by s into gs
  let s = gs.Key
  orderby s.Code 
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),

    from g in gs
    group g.c by g.cy into gcy
    let cy = gcy.Key
    orderby cy.Name
    select new XElement("County",
      new XAttribute("Name", cy.Name),

      from c in gcy
      orderby c.Name
      select new XElement("City",
        new XAttribute("Name", c.Name)
      )
    )
  )
);
只有影子陪我不离不弃 2024-08-10 04:18:37

我认为你有一个好的开始。您可以将有关国家/地区和州的信息添加到您的城市列表中,然后对它们进行分组,从而避免第二次加入和过滤。
您甚至可以在一个大型 linq 查询中执行此操作。很难准确地编写您需要的内容,因为您有自己的类,但这里有一些与文件和文件夹类似的内容(您需要添加另一个级别):

dirs = new List<DirectoryInfo>();
dirs.Add(new DirectoryInfo("c:\\"));
dirs.Add(new DirectoryInfo("c:\\windows\\"));

var a = from directory in dirs
        from file in directory.GetFiles()
        where file.Name.StartsWith("a")
        group file by directory.Name into fileGroup
        select new XElement("Directory", new XAttribute("path", fileGroup.Key),
            from f in fileGroup
            select new XElement("File", f.Name)
            );

XDocument doc = new XDocument(new XElement("Folders", a));

生成 XML:

<Folders>
  <Directory path="c:\">
    <File>ActiveDirectoryService.cs</File>
    <File>ApplicationTemplateCore.wsp</File>
    <File>AUTOEXEC.BAT</File>
  </Directory>
  <Directory path="windows">
    <File>adfs.msp</File>
    <File>adminscript2nd.exe</File>
    <File>aspnetocm.log</File>
  </Directory>
</Folders>

同样,这里的关键是使用 对结果进行分组

I think you have a good start. You can add information about countries and states to your Cities list, and then group 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):

dirs = new List<DirectoryInfo>();
dirs.Add(new DirectoryInfo("c:\\"));
dirs.Add(new DirectoryInfo("c:\\windows\\"));

var a = from directory in dirs
        from file in directory.GetFiles()
        where file.Name.StartsWith("a")
        group file by directory.Name into fileGroup
        select new XElement("Directory", new XAttribute("path", fileGroup.Key),
            from f in fileGroup
            select new XElement("File", f.Name)
            );

XDocument doc = new XDocument(new XElement("Folders", a));

Resulting in the XML:

<Folders>
  <Directory path="c:\">
    <File>ActiveDirectoryService.cs</File>
    <File>ApplicationTemplateCore.wsp</File>
    <File>AUTOEXEC.BAT</File>
  </Directory>
  <Directory path="windows">
    <File>adfs.msp</File>
    <File>adminscript2nd.exe</File>
    <File>aspnetocm.log</File>
  </Directory>
</Folders>

Again, the key here is to use group by on the results.

扮仙女 2024-08-10 04:18:37

一种方法是:首先使用所有正确的内部联接创建查询,然后使用 Distinct() 过滤器创建外部分组,然后使用 where< 从分组创建 XML /code> 子句来加入它们。因此:

var Cities = from s in LinqUtils.GetTable<State>()
             from cy in s.Counties
             from c in cy.Cities
             where c.Name.StartsWith("Y")
             select c;

var States = Cities.Select(c => c.County.State).Distinct();
var Counties = Cities.Select(c => c.County).Distinct();

XElement xml = new XElement("States",
  from s in States
  orderby s.Code
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),
    from cy in Counties
    where cy.StateCode == s.Code
    orderby cy.Name
    select new XElement("County",
      new XAttribute("Name", cy.Name),
      from c in Cities
      where c.CountyID == cy.ID
      orderby c.Name
      select new XElement("City",
        new XAttribute("Name", c.Name)
      )
    )
  )
);

它有效,但不知何故我感觉有更好的方法......

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 a where clause to join them. Thus:

var Cities = from s in LinqUtils.GetTable<State>()
             from cy in s.Counties
             from c in cy.Cities
             where c.Name.StartsWith("Y")
             select c;

var States = Cities.Select(c => c.County.State).Distinct();
var Counties = Cities.Select(c => c.County).Distinct();

XElement xml = new XElement("States",
  from s in States
  orderby s.Code
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),
    from cy in Counties
    where cy.StateCode == s.Code
    orderby cy.Name
    select new XElement("County",
      new XAttribute("Name", cy.Name),
      from c in Cities
      where c.CountyID == cy.ID
      orderby c.Name
      select new XElement("City",
        new XAttribute("Name", c.Name)
      )
    )
  )
);

It works, but somehow I have the feeling that there's a better way...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文