【问题标题】:VB.Net and XDocument - Group ByVB.Net 和 XDocument - 分组方式
【发布时间】:2013-06-24 18:48:49
【问题描述】:

我有以下代码需要修改为group by offer,但我不确定如何在此查询中添加group by 部分。

有什么想法吗?

propertyInfo = myXmlFile.Descendants("offer").Descendants("site") _
    .Where(Function(f) _
    f.Attributes("code").First.Value = Location.Text) _
    .Select(Function(f) New With { _
    .LOCATION = f.Attributes("code").First.Value, _
    .TITLE = f.Parent.Attributes("title").First.Value, _
    .OFFER = f.Parent.Attributes("offerid").First.Value, _
    .POPUPDESC = f.Parent.Descendants("short_desc").Value

示例 XML

<offers>
  <offer title="Offer title" offerid="AS32">
    <short_desc><![CDATA[
        <div>Your HTML's body <a href='dfgdfgdfgdfg.html'>Offer title</a>
        </div>
    ]]></short_desc>

    <site code="CO">
      <year_week_start>201344</year_week_start>
      <year_week_end>201414</year_week_end>
    </site>

    <site code="FH">
      <year_week_start>201446</year_week_start>
      <year_week_end>201450</year_week_end>
    </site>
  </offer>
</offers>

【问题讨论】:

  • 您真的需要使用 LINQ 方法语法吗?我认为对于像分组查询语法msdn.microsoft.com/en-us/library/bb531412.aspx 这样的任务更容易处理。 XML 的样本会有所帮助。是否要按属性offerid 分组?

标签: asp.net xml vb.net linq-to-xml


【解决方案1】:

我认为你需要在分组后更具体地说明你想要什么。分组很容易,但是你会得到一个可枚举的组:

propertyInfo = myXmlFile.Descendants("offer").Descendants("site") _
    .Where(Function(f) _
    f.Attributes("code").First.Value = Location.Text) _
    .Select(Function(f) New With { _
    .LOCATION = f.Attributes("code").First.Value, _
    .TITLE = f.Parent.Attributes("title").First.Value, _
    .OFFER = f.Parent.Attributes("offerid").First.Value, _
    .POPUPDESC = f.Parent.Descendants("short_desc").Value) _
    .GroupBy(Function(f) f.OFFER)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多