开发者

Linq to XML Remove multiple returns

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜