2016-05-25 34 views
1

我希望把這個:如何把一個平面物體的集合爲對象的集合與子集合

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方法可以執行此操作。

+3

的Linq:['GroupBy'(HTTPS: //msdn.microsoft.com/en-us/library/bb545971.aspx) –

+0

這樣做更有意義謝謝! – Robert

回答

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

} 
相關問題