如何将平面对象的集合转换为具有子集合的对象的集合

时间:2016-05-25 16:12:05

标签: c# linq

我想转此:

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 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; }
}

我想我应该在这里使用SelectMany,但我不确定用法。我知道我可以使用循环并单独分配它们,但我确信有一种LINQ方法可以做到这一点。

2 个答案:

答案 0 :(得分:1)

如果您有TopicFromDatabase的集合,则可以按公共字段进行分组,并将每个组投影到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
                                  })
                  }

答案 1 :(得分:0)

试试这个

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; }
    }

}