Linq to XML Noob question - excellent and sorted by attributes

I'm just starting to use Linq for XML, and I have a simple document with these entries:

<record date="6/27/2002" symbol="DG" price="15.00" />

I need a list of different characters as strings.

This gives me an unordered list of all attributes, but I'm stuck

var query =
  from e in xml.Elements()
  select e.Attribute("symbol");

How can this be changed to give me what I want?

+3
source share
2 answers

I would do this with lambda syntax:

var query = xml.Elements()
               .Select(e => (string)e.Attribute("symbol"))
               .Distinct()
               .OrderBy(x=>x);
+2
source

What about:

    var query = (from e in xml.Elements()
                 let symbol = (string)e.Attribute("symbol")
                 where symbol != null
                 orderby symbol
                 select symbol).Distinct();
+7
source

Source: https://habr.com/ru/post/1710911/


All Articles