2

I'm having a little trouble converting some LINQ to VB. I've taken a pass at figuring it out but I've been unsuccessful this moring thus far.

 var feeds = 
  from feed in feedXML.Descendants("item")
  select new
  {
    Date = DateTime.Parse(feed.Element("pubDate").Value)
                   .ToShortDateString(),
    Title = feed.Element("title").Value,
    Link = feed.Element("link").Value,
    Description = feed.Element("description").Value,
  };

Online code translators are not helping, and my unfamiliarly with VB LINQ is not very good. Any help would be greatly appreciated. Thanks!

2 Answers 2

4

You need to:

  1. Use the With keyword when projecting into an anonymous type.
  2. Prefix property names with a dot.
  3. Use a line continuation depending on your version of VB.NET (not needed in VB10). A line continuation is denoted by an underscore at the end of each line.

This yields:

Dim feeds = From feed in feedXML.Descendants("item")
            Select New With
            {
                .Date = DateTime.Parse(feed.Element("pubDate").Value).ToShortDateString(),
                .Title = feed.Element("title").Value,
                .Link = feed.Element("link").Value,
                .Description = feed.Element("description").Value
            }
Sign up to request clarification or add additional context in comments.

1 Comment

wow, 2 almost identical answers (and 2 identical comments), +1 each
3
Dim feeds = From feed In feedXML.Descendants("item") _
            Select New With { _
               .Date = DateTime.Parse(feed.Element("pubDate").Value).ToShortDateString(), _
               .Title = feed.Element("title").Value, _
               .Link = feed.Element("link").Value, _
               .Description = feed.Element("description").Value, _
            }

2 Comments

wow, 2 almost identical answers (and 2 identical comments), +1 each
Wow, I was making that much harder than it needed to be. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.