在同一个 LinqToXml 查询中写入联系人的电话号码?

发布于 2024-12-15 00:06:43 字数 1495 浏览 1 评论 0原文

我有这个代码。

   Dim contact1 As XElement = _
        <contacts>
            <contact>
                <%= From contact In contacts _
                    Select <name><%= contact.Nom %></name> %>
            </contact>
        </contacts>

这段代码实际上选择联系人

有2个对象,联系人和电话。联系人可以保存电话号码列表。这里是课程详细信息...

Public Class Contact

    Private m_Name As String
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

    Private m_Phones As List(Of Phone)
    Public Property Phones() As List(Of Phone)
        Get
            Return m_Phones
        End Get
        Set(ByVal value As List(Of Phone))
            m_Phones = value
        End Set
    End Property

End Class

Public Class Phone

    Private m_Contact As Contact
    Public Property Contact() As Contact
        Get
            Return m_Contact
        End Get
        Set(ByVal value As Contact)
            m_Contact = value
        End Set
    End Property

    Private m_Number As String
    Public Property Number() As String
        Get
            Return m_Number
        End Get
        Set(ByVal value As String)
            m_Number = value
        End Set
    End Property

End Class

我如何在每个电话的 ... 之间包含电话?

奖金(+1):如何也包括类型?

谢谢。

I have this code.

   Dim contact1 As XElement = _
        <contacts>
            <contact>
                <%= From contact In contacts _
                    Select <name><%= contact.Nom %></name> %>
            </contact>
        </contacts>

This code actually select contact

There is 2 objects, contact and phone. The contact can hold a list of phone numbers. Here the class details...

Public Class Contact

    Private m_Name As String
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

    Private m_Phones As List(Of Phone)
    Public Property Phones() As List(Of Phone)
        Get
            Return m_Phones
        End Get
        Set(ByVal value As List(Of Phone))
            m_Phones = value
        End Set
    End Property

End Class

Public Class Phone

    Private m_Contact As Contact
    Public Property Contact() As Contact
        Get
            Return m_Contact
        End Get
        Set(ByVal value As Contact)
            m_Contact = value
        End Set
    End Property

    Private m_Number As String
    Public Property Number() As String
        Get
            Return m_Number
        End Get
        Set(ByVal value As String)
            m_Number = value
        End Set
    End Property

End Class

How do i include phones in this query between <phone type="...">...</phone> for each of them ?

Bonus (+1) : How to include the type also ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶葆Ⅱㄣ 2024-12-22 00:06:43

您还没有清楚地描述您想要什么结果或问题是什么,也许以下代码示例有帮助:

Dim contact1 As New Contact() With {
    .Name = "John Doe",
    .Phones = New List(Of Phone) From {New Phone() With {.Number = "555 12345"}, New Phone() With {.Number = "123 54321"}}
}
Dim contacts As New List(Of Contact)() From {contact1}
Dim contactElements As XElement = _
     <contacts>
         <%= From contact In contacts
             Select <contact>
                        <name><%= contact.Name %></name>
                        <phones>
                            <%= From phone In contact.Phones
                                Select <phone><%= phone.Number %></phone>
                            %>
                        </phones>
                    </contact>
         %>
     </contacts>

输出是

<contacts>
  <contact>
    <name>John Doe</name>
    <phones>
      <phone>555 12345</phone>
      <phone>123 54321</phone>
    </phones>
  </contact>
</contacts>

You haven't clearly described what result you want or what the problem is, perhaps the following code sample helps:

Dim contact1 As New Contact() With {
    .Name = "John Doe",
    .Phones = New List(Of Phone) From {New Phone() With {.Number = "555 12345"}, New Phone() With {.Number = "123 54321"}}
}
Dim contacts As New List(Of Contact)() From {contact1}
Dim contactElements As XElement = _
     <contacts>
         <%= From contact In contacts
             Select <contact>
                        <name><%= contact.Name %></name>
                        <phones>
                            <%= From phone In contact.Phones
                                Select <phone><%= phone.Number %></phone>
                            %>
                        </phones>
                    </contact>
         %>
     </contacts>

The output is

<contacts>
  <contact>
    <name>John Doe</name>
    <phones>
      <phone>555 12345</phone>
      <phone>123 54321</phone>
    </phones>
  </contact>
</contacts>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文