I have to fill in some lists in while loop as:
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();
The data in SQL table looks like:
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
... | ... | ... | ... | ... | ...
The issue is each time loop iterates, new lists _PaperID
, _CoAuthorID
and _VenueID
are created, which is not desired. As we have a check if(author == null)
, then to create a new author, similarly I want to check if a list for _PaperID
exists for an author e.g. for Author_ID = 677
, then to Add in same list until Author_ID
get changed.
Also until the Author_ID = 677
, the list _eAuthors
should have Count = 1
I'm attaching some images to refine the problem.
Image 1: Showing eAuthors
Count = 3, Attributes
Count = 3 for AuthorID = 677, while 3 of iterations passed whereas eAuthors
Count should = 1.
Image 2: Showing Individual Attribute lists for each row, as in 3rd iteration the Attribute e.g. CoAuthorID
, the Count = 1, whereas it should be = 3 while in 3rd iteration and same for rest of the Attributes