How to write this nested Linq To Xml query?

XElement myElement = XElement. Parse(xmlstring); int resultValue = 17; int age = 26; string genderValue = "female"; IEnumerable query = myElement. Descendants("ResultValue") .

Where(rv => ((int)rv. Attribute("low")) ((int)rv. Attribute("high")) >= resultValue) .

Where(rv => rv. Ancestors("Age") . Any(a => ((int) a.

Attribute("low")) = age) ) . Where(rv => ((string)rv. Ancestors("Gender").Single().

Attribute("type")) == genderValue) . Select(rv => rv. Ancestors("Result").Single().

Element("Description"). Value); foreach (string x in query) Console. WriteLine(x) The idea is that you can imagine a row-column shape where each row is a ResultValue.

Each result value has a single parent of Age, a single parent of Gender, an a single parent of Result ResultValue. Low ResultValue. Low Age.

Type Result. Description In fact, one can project the above xml into that shape: var query2 = myElement. Descendants("ResultValue") .

Select(rv => new { ResultValue = rv, Age = rv. Ancestors("Age"), Gender = rv. Ancestors("Gender"), Result = rv.

Ancestors("Result") }) . Select(x => new XElement("Data", new XAttribute("ResultValue. Low", (int)x.ResultValue.

Attribute("low")), new XAttribute("ResultValue. (int)x.ResultValue. Attribute("high")), new XAttribute("Age.

Low", (int)x.Age. Attributes("low").Single()), new XAttribute("Age.(int)x.Age. Attributes("high").Single()), new XAttribute("Gender.

Type", (string) x.Gender. Attributes("type").Single()), new XAttribute("Result. Description", (string) x.Result.

Elements("Description").Single()) )); foreach (XElement x in query2) Console. WriteLine(x).

XElement myElement = XElement. Parse(xmlstring); int resultValue = 17; int age = 26; string genderValue = "female"; IEnumerable query = myElement. Descendants("ResultValue") .

Where(rv => ((int)rv. Attribute("low")) ((int)rv. Attribute("high")) >= resultValue) .

Where(rv => rv. Ancestors("Age") . Any(a => ((int) a.

Attribute("low")) = age) ) . Where(rv => ((string)rv. Ancestors("Gender").Single().

Attribute("type")) == genderValue) . Select(rv => rv. Ancestors("Result").Single().

Element("Description"). Value); foreach (string x in query) Console. WriteLine(x); The idea is that you can imagine a row-column shape where each row is a ResultValue.

Each result value has a single parent of Age, a single parent of Gender, an a single parent of Result. ResultValue. Low ResultValue.

Low Age. Type Result. Description In fact, one can project the above xml into that shape: var query2 = myElement.

Descendants("ResultValue") . Select(rv => new { ResultValue = rv, Age = rv. Ancestors("Age"), Gender = rv.

Ancestors("Gender"), Result = rv. Ancestors("Result") }) . Select(x => new XElement("Data", new XAttribute("ResultValue.

Low", (int)x.ResultValue. Attribute("low")), new XAttribute("ResultValue.(int)x.ResultValue. Attribute("high")), new XAttribute("Age.

Low", (int)x.Age. Attributes("low").Single()), new XAttribute("Age.(int)x.Age. Attributes("high").Single()), new XAttribute("Gender.

Type", (string) x.Gender. Attributes("type").Single()), new XAttribute("Result. Description", (string) x.Result.

Elements("Description").Single()) )); foreach (XElement x in query2) Console. WriteLine(x).

Thanks alot for your help! – Felix Jan 9 '09 at 14:43.

C# - How to write this nested Linq To Xml query - Stack Overflow.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions