访问自定义类列表的成员

时间:2016-06-11 13:54:41

标签: c# list

我有一个班级Author.cs

public class Author
{
   public Author()
   { }  

   public int AuthorID { get; set; }
   public string AuthorName { get; set; }
   public List<AuthorAttributes> Attributes { get; set; }  
   // ...member methods...
}  

和一个班级AuthorAttributes.cs

public class AuthorAttributes  
{  
   public AuthorAttributes()  
   { }  

   public List<int> PaperID = new List<int>();     // public int PaperID;
   public List<int> CoAuthorID = new List<int>();  // public int CoAuthorID;
   public List<int> VenueID = new List<int>();     // public int VenueID;
   public int Year { get; set; } 
}

每个Author都有AuthorAttributes,例如对于AuthorID = 677,某些PaperID中的CoAuthorIDVenueIDYear个数量为:

Author_ID | Author_Name | Paper_ID | CoAuthor_ID | Venue_ID | Year
------------------------------------------------------------------
677       | Nuno Vas    | 812229   | 901706      | 64309    | 2005  
677       | Nuno Vas    | 812486   | 901706      | 65182    | 2005  
677       | Nuno Vas    | 818273   | 901706      | 185787   | 2005  
677       | Nuno Vas    | 975105   | 901706      | 113930   | 2007  
677       | Nuno Vas    | 975105   | 1695352     | 113930   | 2007  
...       | ...         | ...      | ...         | ...      | ...  

我希望将这些数据存储在C#变量中,将其作为Author_IDAuthor_Name添加到类Author中,将其余字段存储在类AuthorAttributes中,然后列出{如上所示,{1}}与AuthorAttributes课程中的每个Author_ID相关联。

当我从SQL Server数据库中读取数据时,我必须逐个读取行,这就是为什么我将类Author.cs的属性声明为AuthorAttributes,而实际上每个int将在某些Author中包含PapersCoAuthorsVenues的列表。

编辑

我想要声明类Years的对象,例如

Author.cs

然后对象Author author = new Author(); 应包含如下:
author中的Author_ID AuthorID中的Author_Name AuthorName中的Paper_ID Attributes中的CoAuthor_ID Attributes中的Venue_ID Attributes

中的Year

数据库阅读示例代码

Attributes

看起来while (myReader_1.Read()) { int authorID = myReader_1.GetInt32(0); Author author = eAthors.FirstOrDefault(_a => _a._AuthorID == _authorID); if (author == null) { author = new Author { AuthorID = authorID, AuthorName = myReader_1.GetString(1), Attributes = new List<AuthorAttributes>() }; eAuthors.Add(author); } author.Attributes.Add(new AuthorAttributes { PaperID = new List<int>() { myReader_1.GetInt32(2) }, CoAuthorID = new List<int>() { myReader_1.GetInt32(3) }, VenueID = new List<int>() { myReader_1.GetInt32(4) }, Year = myReader_1.GetInt32(5), } ); } 是一个列表并且有意义,但每个单独的属性本身也是一个列表Attributes s,Paper_ID s和CoAuthor_ID s更多某个Venue_ID

中的Author_ID = 677只有一个

如何将班级Year的属性定义为AuthorAttributes int

1 个答案:

答案 0 :(得分:1)

正如您所说,每个单独的属性本身都是一个列表,因此您应该将它们定义为int列表,甚至是某些ID包含字符的字符串列表。 我猜数据库阅读的代码示例应该更像:

    while (myReader_1.Read())
    {
       int authorID = myReader_1.GetInt32(0);
       Author author = eAthors.FirstOrDefault(_a => _a._AuthorID == _authorID);
       if (author == null)
       {
          author = new Author
                                {
                                    AuthorID = authorID,
                                    AuthorName = myReader_1.GetString(1),
                                    Attributes = new List<AuthorAttributes>()
                                };

var attribute = new AuthorAttributes()
{
PaperID = new List<int>(),
CoAuthorID = new List<int>(),
VenueID = new List<int>()
};

attribute.PaperID.Add(myReader_1.GetInt32(2));
attribute.CoAuthorID.Add(myReader_1.GetInt32(3));
attribute.VenueID.Add(myReader_1.GetInt32(4));
attribute.Year = myReader_1.GetInt32(5);

author.Attributes.Add(attribute);



eAuthors.Add(author);
}
}