1

この例外が何であるかわかりません。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;
4

3 に答える 3

1

LinqからタイプPatientClassにIEnumerableリターン型をキャストしようとしているため、CLRは関連する型ではないため、これを行うことはできません。

そこにLinqを追加して、PatientClassの単一インスタンスを返す必要があります。

于 2012-05-29T18:19:03.300 に答える
1

LINQクエリはIEnumerable<PatientClass>、単一にキャストしようとしているものを返しますPatientClass。クエリからオブジェクトを1つだけ戻したい場合は、そのオブジェクトに対して.First()を呼び出します。

PatientClass template = (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,

                                                   }).First();
于 2012-05-29T18:19:24.833 に答える
1

一般に、LINQクエリは常にIEnumerable<>オブジェクトを返します。あなたの場合はIEnumerable<PatientClass>。コレクションを単一のオブジェクトにフィルタリングするには、追加の操作を実行する必要があります。

//  will return the first `PatientClass` in the collection.  
//   And will throw an exception if the collection is empty.
return template.First(); 

//  will return the first `PatientClass` in the collection or return the default value       
//   (null in this case) if the collection is empty.
return template.FirstOrDefault(); 

// will return the only `PatientClass` in the collection if the collection only has 
//  one object and will throw an exception if it is empty or has more than 1 object.
return template.Single(); 

// will return the only `PatientClass` in the collection if the collection only has 
//  one object, the default value (null in this case) if it is empty and will throw an    
//  exception if it has more than 1 object.
return template.SingleOrDefault(); 

また、メソッドの代わりに、OrDefault()空のときに返される値を制御するDefaultIfEmpty<>()場合は、コレクションが空の場合に値を指定できる拡張メソッドがあります。

// will return the first element in the collection, but if it is empty, 
//  it will return a new Patient class object created with the default constructor instead of a null
return template.DefaultIfEmpty(new PatientClass()).First();
于 2012-05-29T18:25:55.750 に答える