Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I need to use order by in Linq Query. I tried but its not showing any result.
One more requirement after sortinng i need to pick up top 10 rows.

I tried with this code.
Please help
C#
var ac = doc.Root.Descendants("Positions")
                           .OrderBy(item=>item.Element("ClosingMarketValue"))
                           .Select(item=> new {
                                Col1 = item.Element("MSDWSecurityCode").Value,
                                Col2 = item.Element("ClosingMarketValue").Value
                                  });
Posted
Comments
George Swan 17-Jul-15 17:32pm    
Try
.OrderBy(item=>item.Element("ClosingMarketValue").Value)
jinesh sam 18-Jul-15 0:38am    
Thanks:) I missed .Value
even if i use .value result is not expected.
This helps me .OrderBy(item => (float?)item.Element("ClosingMarketValue"))
Matt T Heffron 17-Jul-15 21:26pm    
To get the top 10 use
.Take(10)
after the .Select(....)
Be sure you have the XML namespaces correct.
See my Solution to your other question: http://www.codeproject.com/Questions/1010662/not-able-to-find-elements-with-Descendants-key-wor?arn=0
jinesh sam 18-Jul-15 0:57am    
@Matt Thanks Its works fine
Maciej Los 6-Jan-16 12:00pm    
var ac = doc.Root.Descendants("Positions")
.Select(item=> new {
Col1 = item.Element("MSDWSecurityCode").Value,
Col2 = item.Element("ClosingMarketValue").Value
})
.OrderBy(x=>x.Col2)
.Take(10);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900