Here is what I want to do:
string parseCode = from x in xml.Descendants("LogType")
where x.Attribute("ID").Value == string)ddlHistoryLogDefinitions.SelectedValue
select x.Attribute("ParseCode").Value;
But that gives error: “Cannot implicitly convert type ‘System.Collections.Generic.IEnumerable’ to ‘string'”
There will only be one value for x.Attribute("ParseCode") but it insists on returning type IEnumerable<string>. How can I extract that value into a string ?
EDIT: Thank you for your responses. Here is what worked for me:
string parseCode = (from x in xml.Descendants("LogType")
where x.Attribute("ID").Value == (string) ddlHistoryLogDefinitions.SelectedValue
select (string) x.Attribute("ParseCode").Value).FirstOrDefault();
This trick was wrapping the entire linq query in () before the .FirstOrDefault().
Use
.Singleto select the only result if you know there will be one and only one:Use
.SingleOrDefaultor.FirstIf there might be none or more then one respectively.