我不知道这个例外是什么。就在我以为我掌握了 linq 的窍门时,发生了这样的事情。我得到了这个例外:
Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,TestImplementationIdeas.PatientClass]' to type 'TestImplementationIdeas.PatientClass'.
现在,我知道为什么会抛出这个异常。
PatientClass template = (PatientClass)(from templates in xDocument.Descendants("template").Where
(templates => ((templates.Descendants("element").Attributes("name").ToString() == "EncounterId") && templates.Descendants("element").Attributes("value").ToString() == Enc.ToString())) //elem.XPathSelectElements(string.Format("//templates/template[./elements/element[@name=\"PopulationPatientID\"and @value='{0}' and @enc='{1}']]",PopulationPatID, Enc))
select new PatientClass
{
PatientId = Convert.ToInt32("0" + templates.XPathSelectElement("elements/element[@name='PatientId']").Attribute("value").Value),
EMPIID = Convert.ToInt32("0" + templates.XPathSelectElement("elements/element[@name='EMPIID']").Attribute("value").Value),
//public int PopulationPatientID { get; set; }
FirstName = templates.XPathSelectElement("elements/element[@name='FirstName']").Attribute("value").Value,
LastName = templates.XPathSelectElement("elements/element[@name='LastName']").Attribute("value").Value,
});
return template != null ? template : null;