2017-01-05 7 views
0

プロファイルまたはページに属する投稿を持っていきたいと思います。同じクラスの2つのクラス、1対多の関係

どのようにして2つのクラス以外の同じクラスに関連付けることができますか。

のようなアレンジではどういうことをお勧めしますか?

私を助けてください。

class Profile 
    { 
     public int ID { get; set; } 

     public string Name { get; set; } 

     public List<Post> Posts { get; set; } 
    } 


class Page 
    { 
     public int ID { get; set; } 

     public string Name { get; set; } 

     public List<Post> Posts { get; set; } 
    } 

class Post 
{ 
    public int ID { get; set; } 
    public string Description { get; set; }   
    public ProfileOrPage MyProperty { get; set; } //<<< My Problem Property 
} 

public void GetProfilePost() 
     { 
      List<Post> postList = new List<Post>(); 

      postList.Add(new Post 
      { 
       ID = 1, 
       new ProfileOrPage {  // My Problem Property 
       ID = 3 
       } 
      }); 

      Profile p = new Profile(); 
      p.Posts = postList; 


     } 
+1

コンテキストを追加します。そして、どういう意味で「My Problem Property」? –

答えて

0

継承を使用できます。

class AbstractPage 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 

    public List<Post> Posts { get; set; } 
} 
class Profile : AbstractPage{} 
class Page : AbstractPage{} 
class Post 
{ 
    public int ID { get; set; } 
    public string Description { get; set; }   
    public AbstractPage MyProperty { get; set; } 
} 
関連する問題