Robert
Robert

Reputation: 4406

How to turn a collection of a flat object into a collection of an object with a child collection

I want to turn this:

public partial class TopicFromDatabase
{
    public int TopicID { get; set; }
    public string TopicName { get; set; }
    public int LanguageID { get; set; }
    public string LanguageName { get; set; }
    public int ApplicationID { get; set; }
    public string ApplicationName { get; set; }
    public int ArticleID { get; set; }
    public string Headline { get; set; }
    public bool IsSticky { get; set; }
}

into this:

public class Topic : ITopic
{
    public int TopicId { get; set; }
    public string TopicName { get; set; }
    public int LanguageId { get; set; }
    public int ApplicationId { get; set; }
    public IEnumerable<IArticle> Articles { get; set; }
}

public class Article : IArticle
{
    public int ArticleId { get; set; }
    public string Headline { get; set; }
    public string Content { get; set; }
    public bool IsSticky { get; set; }
}

I think I am supposed to use SelectMany here to do so, but I am not sure of the usage. I know I could use a loop and assign them individually, but I am sure there is a LINQ way to do this.

Upvotes: 1

Views: 52

Answers (2)

jdweng
jdweng

Reputation: 34429

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ConsoleApplication93
{
    class Program
    {
        static void Main(string[] args)
        {
            List<TopicFromDatabase> topics = new List<TopicFromDatabase>(){
                new TopicFromDatabase() {
                    TopicID = 123,
                    TopicName = "abc",
                    LanguageID = 456,
                    LanguageName = "def",
                    ApplicationID = 789,
                    ApplicationName = "ghi",
                    ArticleID = 234,
                    Headline = "jkl",
                    IsSticky = true
                }
            };

            var results = topics.Select(x => new
            {
                topic = new Topic()
                {
                    TopicId = x.TopicID,
                    TopicName = x.TopicName,
                    LanguageId = x.LanguageID,
                    ApplicationId = x.ApplicationID,
                    Articles = new List<IArticle>() {
                        new Article() {
                            ArticleId = x.ArticleID,
                            Headline = x.Headline,
                            IsSticky = x.IsSticky 
                        }
                    }
                }
            });
        }
    }
    public partial class TopicFromDatabase
    {
        public int TopicID { get; set; }
        public string TopicName { get; set; }
        public int LanguageID { get; set; }
        public string LanguageName { get; set; }
        public int ApplicationID { get; set; }
        public string ApplicationName { get; set; }
        public int ArticleID { get; set; }
        public string Headline { get; set; }
        public bool IsSticky { get; set; }
    }
    public class ITopic
    {
    }

    public class Topic : ITopic
    {
        public int TopicId { get; set; }
        public string TopicName { get; set; }
        public int LanguageId { get; set; }
        public int ApplicationId { get; set; }
        public IEnumerable<IArticle> Articles { get; set; }
    }
    public class IArticle
    {
    }

    public class Article : IArticle
    {
        public int ArticleId { get; set; }
        public string Headline { get; set; }
        public string Content { get; set; }
        public bool IsSticky { get; set; }
    }

}

Upvotes: 0

Arturo Menchaca
Arturo Menchaca

Reputation: 15982

If you have a collection of TopicFromDatabase you can group by common fields and project each group into a Topic:

DbSet<TopicFromDatabase> table;

var topics = table.GroupBy(t => new 
                  {
                      t.TopicId,
                      t.TopicName,
                      t.LanguageId,
                      t.ApplicationId
                  })
                  .Select(g => new Topic
                  {
                      TopicId = g.Key.TopicId,
                      TopicName = g.Key.TopicName,
                      LanguageId = g.Key.LanguageId,
                      ApplicationId = g.Key.ApplicationId,
                      Articles = g.Select(a => new Article
                                  {
                                      ArticleId = a.ArticleId,
                                      Headline = a. Headline,
                                      Content = a. Content,
                                      IsSticky = a. IsSticky
                                  })
                  }

Upvotes: 1

Related Questions