在列表读我必须填写一些列表中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