【问题标题】:Linq To Xml Returning an alphebetically sorted distinct list of valuesLinq To Xml返回一个按字母顺序排序的不同值列表
【发布时间】:2018-02-14 12:07:26
【问题描述】:

我一直在 Linqpad 中尝试解决这个问题,但我一直在想念。考虑以下几点:

//<SpeciesSizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// < SpeciesSize >
//  < DisplayCode > CAT </ DisplayCode >
//  < EnglishName > Wolffishes(= Catfishes) nei </ EnglishName >
//      < State > FRE </ State >
//      < Presentation > BMS </ Presentation >
//      < Freshness > SO </ Freshness >
//      < Size > 9 </ Size >
//      < Description > Species not included in the common marketing standards</Description>
//       </SpeciesSize>
//  <SpeciesSize>
//    <DisplayCode>CAT</DisplayCode>
//         <EnglishName>Wolffishes(= Catfishes) nei </ EnglishName >
//          < State > FRE </ State >
//          < Presentation > FIL </ Presentation >
//          < Freshness > SO </ Freshness >
//          < Size > 9 </ Size >
//          < Description > Species not included in the common marketing standards</Description>
// </SpeciesSize>
//  < SpeciesSize >
//  < DisplayCode > FLE </ DisplayCode >
//  < EnglishName > European flounder </ EnglishName >
//     < State > FRE </ State >
//     < Presentation > GUT </ Presentation >
//     < Freshness > E </ Freshness >
//     < Size > 1 </ Size >
//     < Description > According to AnnexII of Council R. 2406 / 96 </ Description >
//      </ SpeciesSize >
// < SpeciesSize >
//       < DisplayCode > GUX </ DisplayCode >
//       < EnglishName > Gurnards, searobins nei</EnglishName>
//       <State>FRO</State>
//       <Presentation>ROE</Presentation>
//       <Freshness>SO</Freshness>
//       <Size>9</Size>
//       <Description>Species not included in the common marketing standards</Description>
//  </SpeciesSize>
//</SpeciesSizes>
static string path = @"C:\Users\dom\Documents\Speciescrossreference.xml";

void Main()
{
    XDocument doc = XDocument.Load(path);

    var names = from n in doc.Root.Descendants("SpeciesSizes")
    group n by n.Element("SpeciesSize") into g
    select new {
        name = g.Element("EnglishName").Distinct().ToList()
    };


    names.Dump();

}

大约有 4000 个 &lt;SpeciesSize&gt; 元素,其中大约有 150 个独特的英文名称。

我已经阅读了关于 SO 的各种答案,并从中提取了一些信息来拼凑我在这里所拥有的东西,但显然我无法理解这些答案中展示的所有语法。

是否有人能够告诉我我做错了什么,以便我以后能够构造可能涉及对不同元素进行分组的其他查询(针对相同的 xml)。

鉴于代码示例顶部的示例 xml,我将查看 atr 返回以下内容:

  • 欧洲比目鱼
  • Gurnards, searobins nei
  • 狼鱼(= 鲶鱼) nei

【问题讨论】:

  • @mjwills 我在代码部分的顶部添加了更多 xml,并在问题末尾显示了我想要的内容。值得注意的是,原来的xml是按DisplayName排序的,而不是EnglishName。
  • 您需要告诉我们您的期望(问题是什么)。另外我的假设是你不想要group bywhere 条件。如果您进行分组,它会将事物分组,例如 group by Size 会将物种分组,因此您可能有三个组(大小 1、2、3)。
  • 这就是我的问题的重点,各种 SpeciesSize 元素中的每一个都包含几乎(但不完全)相似的值。我想获得出现的那些 EnglishNames 的不同列表。从这里我也可以稍后继续对不同的语言变体进行排序,但首先我会先让英语正确。

标签: c# linq-to-xml


【解决方案1】:

如果您的示例中没有空格,您可以使用:

var names = from n in doc.Root.Descendants("SpeciesSize")
            group n by n.Element("EnglishName").Value into g
            select new
            {
                name = g.Key
            };

更新@CodingYoshi:
是的,这是正确的。如果只是读取名称,您可以执行以下操作:

var names = doc.Root.Descendants("EnglishName")
    .Select(item => item.Value)
    .Distinct()
    .OrderBy(item => item);

【讨论】:

  • 非常感谢,这绝对是唯一名称的列表,我应该能够对它们进行排序。
  • 为什么不使用 distinct?您正在分组,确定它有效,但您不需要它。
【解决方案2】:

为了完整起见,如果其他人谈到这一点。接受的答案为我指明了正确的方向,产生了一个独特的英文名称列表。不过,我需要按字母顺序排序的列表,经过一番对上述答案的轻微改编后,我得到了我所追求的;

var names  = from n in doc.Root.Descendants("SpeciesSize")
            orderby n.Element("EnglishName").Value
            group n by  n.Element("EnglishName").Value  into g

            select new
            {
                name =  g.Key
            };

编辑

根据下面关于正确处理的评论,我决定弄清楚如何去做。

这是我想出的最好的方法,它只使用 Distinct 并完全避免 group by 子句。

var names = (from n in doc.Root.Descendants("SpeciesSize")
            orderby n.Element("EnglishName").Value
            select new
            {
                name = n.Element("EnglishName").Value
            }).Distinct();

【讨论】:

  • 您不需要分组。如果您需要不同,请使用 distinct。但很高兴你让它工作。现在你需要正确地做到这一点。
  • 经过多次修改和阅读各种零碎的文章,我希望我最终添加的修订版使 linq 尽可能简洁。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 2015-01-18
  • 2020-03-29
相关资源
最近更新 更多