Saturday, March 6, 2010

aspx linq get all unique items from an xml

        XDocument xDoc = XDocument.Load(Server.MapPath("App_Data/MyCollections.xml"));
        List <string>productTypes =
            xDoc.Descendants("CollectionItem")
            .SelectMany(ci => ci.Descendants("ProductType"))
            .Select(pt => pt.Value).Distinct().ToList();


this will return a List<> with all the Distinct ProductType Values under the CollectionItem node of the xml file.
.SelectMany because we want all of the values for ProductType
.Select says we want the Value of the node and only want Distinct ones.

No comments: