VB.net 和 C# 中的 IEnumerable 有何不同?

发布于 2024-12-14 21:44:30 字数 2928 浏览 2 评论 0原文

我正在将 http://customfeedaggregator.codeplex.com/ 移植到 c#,让自己用 C# 和 WPF 进行思考。

我陷入了 IEnumerable 中的一个问题。

有一个类 - blogpost.vb

'Represents a single blog post
Class BlogPost

    Private _title As String
    Private _datePublished As DateTime
    Private _url As Uri
    Private _category As String

    Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Property DatePublished() As DateTime
        Get
            Return _datePublished
        End Get
        Set(ByVal value As DateTime)
            _datePublished = value
        End Set
    End Property

    Property Url() As Uri
        Get
            Return _url
        End Get
        Set(ByVal value As Uri)
            _url = value
        End Set
    End Property

    Property Category() As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
        End Set
    End Property


End Class

和一个共享函数,用于检索提要并将其转换为博客文章。

Shared Function RetrieveFeeds(ByVal Address As String) As IEnumerable(Of BlogPost)

        Dim doc As XDocument = XDocument.Load(Address)

        Dim query = From item In doc...<item> _
        Let DataPubblicazione = CDate(item.<pubDate>.Value).ToLocalTime _
        Let TitoloPost = item.<title>.Value _
        Let Url = item.<link>.Value _
        Let Categoria = item.<category>.Value _
            Order By DataPubblicazione Descending _
            Select New BlogPost With _
            {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
             .Url = New Uri(Url), .Category = Categoria}

        Return query
    End Function

班级是一个标准,所以这不是问题。但 RetreiveFeeds 很困难。

这是我的 C# 版本:

 public static IEnumerable<BlogPost> RetrieveFeeds(string Address)
    {
        XDocument doc = XDocument.Load(Address);

        var query = from item in doc.Descendants("item")
                    let DataPubblicazione = Convert.ToDateTime(item.Attribute("pubDate").Value)
                    let TitoloPost = item.Attribute("title").Value 
                    let Url = item.Attribute("link").Value 
                    let Categoria = item.Attribute("category").Value 
                    orderby DataPubblicazione descending 
                    select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  Url, Categoria};
        return query;
    }

“选择新博客文章”部分显示的错误是:

无法使用集合初始值设定项初始化类型“FeedMe.BlogPost”,因为它未实现“System.Collections.IEnumerable”。

那么,我是否需要在我的数据类中显式实现 IEnumerable ?或者我的 C# 端口代码错误?这是 VB.net 和 C# 之间的区别吗?

I am porting http://customfeedaggregator.codeplex.com/ to c#, to make myself think in C# and WPF.

I am stuck on an issue in IEnumerable.

There's a class - blogpost.vb

'Represents a single blog post
Class BlogPost

    Private _title As String
    Private _datePublished As DateTime
    Private _url As Uri
    Private _category As String

    Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Property DatePublished() As DateTime
        Get
            Return _datePublished
        End Get
        Set(ByVal value As DateTime)
            _datePublished = value
        End Set
    End Property

    Property Url() As Uri
        Get
            Return _url
        End Get
        Set(ByVal value As Uri)
            _url = value
        End Set
    End Property

    Property Category() As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
        End Set
    End Property


End Class

and a shared function that retrieves feeds and converts them to blogposts.

Shared Function RetrieveFeeds(ByVal Address As String) As IEnumerable(Of BlogPost)

        Dim doc As XDocument = XDocument.Load(Address)

        Dim query = From item In doc...<item> _
        Let DataPubblicazione = CDate(item.<pubDate>.Value).ToLocalTime _
        Let TitoloPost = item.<title>.Value _
        Let Url = item.<link>.Value _
        Let Categoria = item.<category>.Value _
            Order By DataPubblicazione Descending _
            Select New BlogPost With _
            {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
             .Url = New Uri(Url), .Category = Categoria}

        Return query
    End Function

The class is a standard, so that wasn't an issue. But the RetreiveFeeds is being difficult.

Here's my C# version:

 public static IEnumerable<BlogPost> RetrieveFeeds(string Address)
    {
        XDocument doc = XDocument.Load(Address);

        var query = from item in doc.Descendants("item")
                    let DataPubblicazione = Convert.ToDateTime(item.Attribute("pubDate").Value)
                    let TitoloPost = item.Attribute("title").Value 
                    let Url = item.Attribute("link").Value 
                    let Categoria = item.Attribute("category").Value 
                    orderby DataPubblicazione descending 
                    select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  Url, Categoria};
        return query;
    }

The error shown at the part where it says Select New Blogpost is:

Cannot initialize type 'FeedMe.BlogPost' with a collection initializer because it does not implement 'System.Collections.IEnumerable'.

So, do I need to explicitly implement IEnumerable in my dataclass? or is my C# Port code wrong? Is this a difference between VB.net and C#?

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

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

发布评论

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

评论(1

等风也等你 2024-12-21 21:44:30

实际上,C# 和 VB.NET 之间的正确语法非常相似:

原始 C#:

select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  
                     Url, Categoria};

更正的 C#:

select new BlogPost {DatePublished = DataPubblicazione , 
                     Title = EscapeXML(TitoloPost),  
                     Url = new Uri(Url), 
                     Category = Categoria};

原始 VB.NET:

Select New BlogPost With _
    {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
     .Url = New Uri(Url), .Category = Categoria}

使用对象初始值设定项声明 new BlogPost 时,您需要命名参数。

Actually the correct syntax is very similar between C# and VB.NET:

Original C#:

select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  
                     Url, Categoria};

Corrected C#:

select new BlogPost {DatePublished = DataPubblicazione , 
                     Title = EscapeXML(TitoloPost),  
                     Url = new Uri(Url), 
                     Category = Categoria};

Original VB.NET:

Select New BlogPost With _
    {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
     .Url = New Uri(Url), .Category = Categoria}

When declaring a new BlogPost with an object initializer you need to name the parameters.

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