C
C#13mo ago
stigzler

Split IEnumerable<XElement> into separate lists by Attribute Value

I have a variable set of XEelements (IEnumerable<XElement>) like so:
<genre id="2892" nomcourt="" principale="0" parentid="2646" langue="en">Shooter / Plane, 1st person</genre>
<genre id="2892" nomcourt="" principale="0" parentid="2646" langue="fr">Tir / Avion, 1ere personne</genre>
<genre id="2892" nomcourt="" principale="0" parentid="2646" langue="de">Shooter / Flugzeug, 1st Pers.</genre>
<genre id="2646" nomcourt="0100" principale="1" parentid="0" langue="en">Shooter</genre>
<genre id="2646" nomcourt="0100" principale="1" parentid="0" langue="fr">Tir</genre>
<genre id="2892" nomcourt="" principale="0" parentid="2646" langue="en">Shooter / Plane, 1st person</genre>
<genre id="2892" nomcourt="" principale="0" parentid="2646" langue="fr">Tir / Avion, 1ere personne</genre>
<genre id="2892" nomcourt="" principale="0" parentid="2646" langue="de">Shooter / Flugzeug, 1st Pers.</genre>
<genre id="2646" nomcourt="0100" principale="1" parentid="0" langue="en">Shooter</genre>
<genre id="2646" nomcourt="0100" principale="1" parentid="0" langue="fr">Tir</genre>
I don't know the number of elements, number of unique ids or any of the Attribute values in advance. I want to split this IEnumerable into x number of collections (can be list or ienumerable) by the id Attribute. In this example, I would get two collections, split by the ids 2892 and 2646. What's the tidyest way to achieve this? I'm guessing it's via Linq, but cannot figure for the life of me how...
2 Replies
viceroypenguin
viceroypenguin13mo ago
elements
.GroupBy(x => x.Attribute("id").Value)
elements
.GroupBy(x => x.Attribute("id").Value)
stigzler
stigzlerOP13mo ago
You nailed it - thanks dude
Want results from more Discord servers?
Add your server