Linq To Xml 返回按字母顺序排序的不同值列表

Linq To Xml Returning an alphebetically sorted distinct list of values

我一直在 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 个 <SpeciesSize> 元素,其中约有 150 个唯一英文名称。

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

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

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

如果你的例子中没有空格,你可以使用这个:

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);

为了完整起见,如果还有其他人来到这里。接受的答案为我指明了正确的方向,生成了一个独特的英文名称列表。不过,我需要该列表按字母顺序排序,经过对上述答案的轻微改编后,我得到了我想要的东西;

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();