在列表讀我必須填寫一些列表中while循環:使用SqlDataReader的C#
while (_myReader_1.Read())
{
_Row_Counter++;
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);
_eAthors.Add(_author);
}
_myReader_1.Close();
在SQL表中的數據是這樣的:
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
... | ... | ... | ... | ... | ...
的問題是,在每次循環迭代,新列表_PaperID
,_CoAuthorID
和_VenueID
被創建,這是不期望的。因爲我們有一個支票if(author == null)
,然後創建一個新的作者,同樣我想檢查一個作者是否存在_PaperID
的列表,例如,爲Author_ID = 677
,然後到在相同列表中添加,直到Author_ID
被更改。
而且直到Author_ID = 677
,列表_eAuthors
應該有Count = 1
我附加了一些圖片,以細化的問題。
圖1:顯示eAuthors
計數= 3,Attributes
計數= 3的AuthorID = 677,而迭代的3傳遞而eAuthors
計數應= 1
圖2:顯示每行的單獨屬性列表,如在第三次迭代中的屬性例如CoAuthorID
,計數= 1,而應該是= 3,而在第3次迭代和同爲的Attributes
這有嚴重的問題。您如何知道CoAuthor_ID 1695352與哪個論文相關聯?或者當年移至2007年時。您需要更正式的數據結構。 – Paparazzi
我需要跟蹤作者及其屬性,即相應年份中的紙張,作者和地點,這裏不需要CoAuthor_ID 1695352與 – maliks
相關聯的文章那麼您甚至在這裏沒有*相應*年。爲什麼新作者沒有初始化這些列表?看起來很sl to。 – Paparazzi