从 VB.NET 中的 LINQ 查询返回匿名类型

发布于 2024-10-26 17:31:10 字数 1045 浏览 0 评论 0原文

我正在使用转发器控件使用 RSS 提要在我的网站上显示。我想知道 VB 中是否可以从 linq 查询返回匿名类型,而不是强类型 RSSItem 的集合。我知道这在 C# 中是可能的,但是无法找到 VB 的等效项。

Public Class RSSItem
    Public Property Title As String
    Public Property Link As String
    Public Property Content As String
    Public Property Description As String
    Public Property pubDate As String
    Public Property category As String
End Class

    Dim feedXML As XDocument = XDocument.Load("http://myrssfeed.com/rss.xml")
    Dim xns As XNamespace = "http://purl.org/rss/1.0/modules/content/"

    Dim feeds = From feed In feedXML.Descendants("item") _
                Select New RSSItem With _
                       {.Title = feed.Element("title"),
                        .Link = feed.Element("link"),
                        .Content = feed.Element(xns.GetName("encoded")).Value,
                        .Description = feed.Element("description"),
                        .pubDate = feed.Element("pubDate"),
                        .category = GetCategories(feed.Elements("category"))}

I am consuming an RSS feed to display on my website using a repeater control. I was wondering if it's possible in VB to return an anonymous type from my linq query rather than a collection of strongly typed RSSItems. I know this is possible in C#, however haven't been able to work out a VB equivalent.

Public Class RSSItem
    Public Property Title As String
    Public Property Link As String
    Public Property Content As String
    Public Property Description As String
    Public Property pubDate As String
    Public Property category As String
End Class

    Dim feedXML As XDocument = XDocument.Load("http://myrssfeed.com/rss.xml")
    Dim xns As XNamespace = "http://purl.org/rss/1.0/modules/content/"

    Dim feeds = From feed In feedXML.Descendants("item") _
                Select New RSSItem With _
                       {.Title = feed.Element("title"),
                        .Link = feed.Element("link"),
                        .Content = feed.Element(xns.GetName("encoded")).Value,
                        .Description = feed.Element("description"),
                        .pubDate = feed.Element("pubDate"),
                        .category = GetCategories(feed.Elements("category"))}

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

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

发布评论

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

评论(1

有深☉意 2024-11-02 17:31:10

我相信您可以将 New RSSItem With 更改为 New With。更多详细信息可以在VB 匿名类型 MSDN 页面中找到。

I believe you can change New RSSItem With to New With. More details can be found in the VB Anonymous Types MSDN page.

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