tags:

views:

526

answers:

3

Hi, the following linq statement returns a IOrderedEnumerable:

        var list = from e in ritorno.Elements("dossier")
                              orderby e.Element("name")
                              select e;

How can i convert list to XElement? Thanks

EDIT

list is IOrderedEnumerable<System.Xml.Linq.XElement>

+1  A: 

Do you want a single XElement to contain all the elements in the sequence?

XElement element = new XElement("container", list)

(Obviously change "container" to whatever you want the containing element to be called.)

If that's not what you want, please elaborate.

Jon Skeet
this is exactly what i want, but what you suggested gives me a "At least one object must implement IComparable" error ..
pistacchio
+1  A: 

You could do something like:

XElement xml = new XElement("dossiers", 
                from e in ritorno.Elements("dossier")
                orderby e.Element("name")
                select new XElement("dossier", e.Value));

which essentially is what Jon was trying to say ( I think).

Johan Leino
strangely enough, this also gives me the same error "At least one object must implement IComparable"
pistacchio
Could it be that you must add orderby e.Element("name").Value?
Johan Leino
A: 

Ok, the problem was with the cast of e.Element("name").

The following now works:

        var ritornoOrdinato = from e in segnalazioni.Descendants("dossier")
                              orderby (string)e.Element("ANAG_RAGSOC_CGN")
                              select e;
        return new XElement("NewDataSet", ritornoOrdinato);
pistacchio