简体   繁体   English

IEnumerable在VB.net和C#中是否有所不同?

[英]Is IEnumerable different in VB.net and C#?

I am porting http://customfeedaggregator.codeplex.com/ to c#, to make myself think in C# and WPF. 我正在将http://customfeedaggregator.codeplex.com/移植到c#,以使自己想到C#和WPF。

I am stuck on an issue in IEnumerable . 我陷入IEnumerable一个问题。

There's a class - blogpost.vb 有一个课-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. 但是RetreiveFeeds很难。

Here's my C# version: 这是我的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;
    }

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'. 无法使用集合初始化程序初始化类型“ FeedMe.BlogPost”,因为它未实现“ System.Collections.IEnumerable”。

So, do I need to explicitly implement IEnumerable in my dataclass? 因此,是否需要在数据类中显式实现IEnumerable? or is my C# Port code wrong? 还是我的C#端口代码错误? Is this a difference between VB.net and C#? VB.net和C#之间有区别吗?

Actually the correct syntax is very similar between C# and VB.NET: 实际上,正确的语法在C#和VB.NET之间非常相似:

Original C#: 原始C#:

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

Corrected C#: 更正的C#:

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

Original VB.NET: 原始的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. 使用对象初始化程序声明new BlogPost ,需要命名参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM