Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to XML Noob question - distinct and order by on attributes

Tags:

linq

I'm just getting started using Linq to XML and I have a simple document with records like this:

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

I want a list of distinct symbols as strings, in order.

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 modified to give me what I want?

like image 237
Old Man Avatar asked Jan 29 '26 08:01

Old Man


1 Answers

How about:

    var query = (from e in xml.Elements()
                 let symbol = (string)e.Attribute("symbol")
                 where symbol != null
                 orderby symbol
                 select symbol).Distinct();
like image 166
Marc Gravell Avatar answered Jan 31 '26 13:01

Marc Gravell