开发者

Linq to XML Remove multiple returns

开发者 https://www.devze.com 2023-03-17 15:13 出处:网络
Hi have a Linq Query to extract some information. Following is the part of it. node = \"DocumentClass\";

Hi have a Linq Query to extract some information. Following is the part of it.

 node = "DocumentClass";
 AVariable="Something"
 na="NA";
 var documentClassesScript = (from documentClass in configparentXML.Descendants(node)
                              where documentClass.Attribute("Name").Value.Contains(AVariable)
                              select new Reading开发者_运维百科XmlWithLinq
                                       {
                                        CustomStorageString = documentClass.Element("ValidationPluginAssociations") != null ? documentClass.Descendants("ValidationPluginAssociation").Attributes("CustomStorageString").Single().Value : na,
                                        } 
  ).Distinct();

In Some case i got following error

Sequence contains more than one element

The reason is ValidationPluginAssociations contain more than one ValidationPluginAssociation. I need to distinct and get only one of them. Is there any way to get it.


if there is no need to have single object you can use First:

documentClass.Descendants("ValidationPluginAssociation")
             .Attributes("CustomStorageString").First().Value
0

精彩评论

暂无评论...
验证码 换一张
取 消