【发布时间】: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 个 <SpeciesSize> 元素,其中大约有 150 个独特的英文名称。
我已经阅读了关于 SO 的各种答案,并从中提取了一些信息来拼凑我在这里所拥有的东西,但显然我无法理解这些答案中展示的所有语法。
是否有人能够告诉我我做错了什么,以便我以后能够构造可能涉及对不同元素进行分组的其他查询(针对相同的 xml)。
鉴于代码示例顶部的示例 xml,我将查看 atr 返回以下内容:
- 欧洲比目鱼
- Gurnards, searobins nei
- 狼鱼(= 鲶鱼) nei
【问题讨论】:
-
@mjwills 我在代码部分的顶部添加了更多 xml,并在问题末尾显示了我想要的内容。值得注意的是,原来的xml是按DisplayName排序的,而不是EnglishName。
-
您需要告诉我们您的期望(问题是什么)。另外我的假设是你不想要
group by但where条件。如果您进行分组,它会将事物分组,例如 group bySize会将物种分组,因此您可能有三个组(大小 1、2、3)。 -
这就是我的问题的重点,各种 SpeciesSize 元素中的每一个都包含几乎(但不完全)相似的值。我想获得出现的那些 EnglishNames 的不同列表。从这里我也可以稍后继续对不同的语言变体进行排序,但首先我会先让英语正确。
标签: c# linq-to-xml