Create IEnumerable<T> with one line in LINQ

Pretty straight-forward, but a useful snippet of code nonetheless. 

Given this class:

    public class AggregateContent
    {
 
        public string Title { get; set; }
        public string Description  { get; set; }
      

        public AggregateContent(string Header, string Content)
        {
            Title = Header;
            Description = Content;
        }
    }

You can use line to create an IEnumerable<AggregateContent> like this:

            IEnumerable<AggregateContent> a = 
                LastNNewsItems.Select(p => new AggregateContent(p.NewsTitle, p.HtmlContent));
 
Technorati Tags:
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted @ Friday, February 05, 2010 4:45 PM

Twitter