Last post Oct 01, 2015 03:00 PM by amithashenoy
Oct 01, 2015 02:35 PM|amithashenoy|LINK
Below is code, i want to select attribute with highest value. Can anyone suggest me how to do it
i want to retrieve the greatest Id value.Example in above code i want the value Id=3 to be selected. I tried
var result = root.Element("Samples").Elements("Sample").OrderByDescending (x =>(int)x.Attribute("Id"));
it gives in Desc order again i have to parse the result. SO can anyone tell me if it can be done in single line
Thanks in advance.
Oct 01, 2015 02:47 PM|Mukesh_Kumar|LINK
You can use your code like this to get max value..
var maxValue= Samples.Descendants("Sample").Max(x => (int) x);
XElement element = XElement.Load("../../yourxml.xml");
int max = element.Elements("Sample").Max(l => int.Parse(l.Attribute("Id").Value));
For more refer this links
Hope this will help you.
Oct 01, 2015 03:00 PM|amithashenoy|LINK
Yes it worked. Thanks. i forgot there is method called Max() thank yo so much