在VB.net和C#中IEnumerable有什么不同?

时间:2011-11-10 15:02:53

标签: c# vb.net ienumerable

我正在将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#之间的区别吗?

1 个答案:

答案 0 :(得分:3)

实际上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时,需要为参数命名。