首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从XAttribute中子串LINQ值

如何从XAttribute中子串LINQ值
EN

Stack Overflow用户
提问于 2012-01-11 00:54:15
回答 1查看 167关注 0票数 0

我有一些丑陋的xml(如果我可以改变的话,我会改变的),我需要能够处理它们。我想按XML中的特定属性进行分组,以便进一步处理。问题是有些属性的type=是"A“,有些属性的type=是”A1“,type=是”A2“,等等。我希望所有属性类型值以”A“开头的元素都被分组在一起。

这不起作用,我不知道如何改变才能使其起作用:

代码语言:javascript
复制
    var result = from ele in xdoc.Descendants(Namespace + "item")
                 let item= ele.Attribute("type").Value.Substring(1,1)
                 group ele by item into grp
                 select grp;

当我去掉子字符串时,它工作得很好,但显然不能按我想要的方式进行分组。

错误:

代码语言:javascript
复制
Error has been thrown by the target of an invocation

示例XML:

代码语言:javascript
复制
<items>
 <item type="A" position="0">
   <itemvalue>10</itemvalue>
 </item>
  <item type="A1" position="1">
    <itemvalue>20</itemvalue>
 </item>
  <item type="A2" position="2">
    <itemvalue>30</itemvalue>
 </item>
  <item type="B" position="0">
    <itemvalue>10</itemvalue>
  </item>
   <item type="B1" position="1">
     <itemvalue>20</itemvalue>
  </item>
   <item type="B2" position="2">
     <itemvalue>30</itemvalue>
 </item>
</items>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-11 01:13:43

如果没有编译器在我的手机上输入,我会说你应该把0作为第一个参数传递给Substring,也就是Substring(0,1)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8807152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档