Linq and sql

I

Ilyas

Hi all

I have the following xml file :

<?xml version="1.0" encoding="utf-8" ?>
<root>
<persons>
<person>
<firstname>Mark</firstname>
<surname>Jones</surname>
<age>33</age>
</person>
<person>
<firstname>Adbul</firstname>
<surname>Khan</surname>
<age>30</age>
</person>
<person>
<firstname>Bob</firstname>
<surname>Smith</surname>
<age>31</age>
</person>
</persons>
</root>

I have the following expression

XElement myXml = XElement.Load("Sample.xml");

var allPersonsInOrderOfAge = from person in
myXml.Descendants("person") orderby person.Element("age") select
person;

However when I run this - I get an error saying "At least one object
must implement IComparable"

Can anyone shed any light on this? Is my expression wrong... How can I
correct this problem?

Many thanks
 
D

David R. Longnecker

var allPersonsInOrderOfAge = from person in
myXml.Descendants("person") orderby person.Element("age") select
person;

You need to cast your person.Element("age") to a type so it knows how to
compare the elements:

var allPersonsInOrderOfAge =
from person in myXml.Descendants("person")
orderby (int)person.Element("age")
select person;

Hope this helps.

-dl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top