Getting an invalid cast exception when trying to order a list of objects with linq

Viewed 8419

I'm trying to sort a list of tweets (class: SimpleTweet), which each have ID associated with them (x.ID where x is an object of class SimpleTweet). I'm using linq to sort this, using "OrderByDescending", but am getting an error on the line where I set a new object of type List(Of SimpleTweet) equal to the sorted list. The error I am getting is, "System.InvalidCastException: Unable to cast object of type 'System.Linq.OrderedEnumerable2[SimpleTweet,System.Int64]' to type 'System.Collections.Generic.List1[SimpleTweet]'".

The code:

 <WebMethod()> _
    Public Function GetTweetsByUserID(ByVal userID As Integer) As List(Of SimpleTweet)
        Dim result As New List(Of SimpleTweet)
        Dim urlTwitter As String = "https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name={0}&count=3"
        'Dim twitterfeed As String = utils.GetUserTwitterFeeds(userID, "docphin")
        Dim lq As New lqDFDataContext
        Dim var = lq.web_GetTweetsByUserID(userID).ToList()
        Dim sortedresult As New List(Of SimpleTweet)
        If Not var Is Nothing Then
            For Each twitterfeed In var

                Dim listURL As String = String.Format(urlTwitter, twitterFeed.TweeterFeed)

                Dim tweetXML As XmlDocument = utils.GetXMLForURL(listURL)

                Dim tweetnodelist As XmlNodeList = tweetXML.ChildNodes(1).ChildNodes

                For Each node As XmlNode In tweetnodelist
                    Dim tweet As New SimpleTweet
                    tweet.CreatedAt = node.SelectSingleNode("created_at").InnerText
                    tweet.HTMLText = utils.ReturnTextWithHRefLink(node.SelectSingleNode("text").InnerText)
                    tweet.ID = node.SelectSingleNode("id").InnerText
                    tweet.Name = node.SelectSingleNode("user/name").InnerText
                    tweet.ScreenName = node.SelectSingleNode("user/screen_name").InnerText
                    tweet.Text = node.SelectSingleNode("text").InnerText
                    tweet.UserID = node.SelectSingleNode("user/id").InnerText
                    tweet.ProfileImageURL = node.SelectSingleNode("user/profile_image_url_https").InnerText
                    result.Add(tweet)
                Next
            Next

        sortedresult = result.OrderByDescending(Function(tweet) tweet.ID)  
        End If
        Return sortedresult
    End Function
2 Answers
Related