[英]Accessing members of a custom class 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
,在特定Year
中有PaperID
, CoAuthorID
和VenueID
的數量,例如:
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_ID
和Author_Name
類Author
和在類中的字段的其余AuthorAttributes
然后具有的列表AuthorAttributes
與每個附Author_ID
類Author.cs
如上所示。
當我看到從SQL Server數據庫中的數據,我要讀行一一這就是為什么我宣布類的屬性AuthorAttributes
為int
,而實際上每個Author
將有一個列表Papers
, CoAuthors
和Venues
在某些Years
。
編輯
我想要聲明類Author.cs
的對象,例如
Author author = new Author();
那么對象author
應包含以下內容:
AuthorID
Author_ID
Author_Name
中的AuthorName
Attributes
Paper_ID
Attributes
CoAuthor_ID
Attributes
Venue_ID
Attributes
Year
數據庫讀取的示例代碼
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
是列表,是有意義的,但每個單獨的屬性本身也是一個列表, Paper_ID
S, CoAuthor_ID
S和Venue_ID
s為一個以上的Author_ID = 677
在一定的Year
如何將類AuthorAttributes
屬性定義為int
或 List<int>
?
如您所說,每個單獨的屬性本身都是一個列表,因此您應該將它們定義為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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.