[英]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示例,我將看到返回以下內容:
您的示例中沒有空格,您可以使用以下命令:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.