使用 SqlDataReader C# 读取列表

Reading in list using SqlDataReader C#

我必须在 while 循环中填写一些列表:

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 table 中的数据如下所示:

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 Count = 3,Attributes Count = 3 for AuthorID = 677,而 3 次迭代通过,而 eAuthors 计数应该 = 1。

图 2: 显示每行的单独属性列表,如第 3 次迭代中的属性,例如CoAuthorID,计数 = 1,而在第 3 次迭代中它应该是 = 3,其余 Attributes

假设您的数据结构如下所示:

Author
    AuthorAttributes
        Papers (list)
            PaperID
        CoAuthors (list)
            CoAuthorID
        Venues (list)
            VenueID
        Year

你可以试试这个:

while (_myReader_1.Read())
{
    _Row_Counter++;

    int _authorID = _myReader_1.GetInt32(0);
    string _authorName = _myReader_1.GetString(1);
    int _paperID = _myReader_1.GetInt32(2);
    int _coAuthorID = _myReader_1.GetInt32(3);
    int _venueID = _myReader_1.GetInt32(4);
    int _year = _myReader_1.GetInt32(5);

    Author _author = _eAthors.FirstOrDefault(_a => _a._AuthorID == _authorID);
    if (_author == null)
    {
        _author = new Author
        {
             _AuthorID = _authorID,
             _AuthorName = _authorName,
             _AuthorAttributes = new AuthorAttributes
            {
                _Papers = new List<int>(),
                _Venues = new List<int>(),
                _Year = _year,
                _CoAuthors = new List<int>()
            }
        };  
        _eAthors.Add(_author); // only add if author not found
    }  

    if ( !_author._AuthorAttributes._Papers.Contains( _paperID ) )
        _author._AuthorAttributes._Papers.Add( _paperID );
    if ( !_author._AuthorAttributes._CoAuthors.Contains( _coAuthorID ) )
        _author._AuthorAttributes._CoAuthors.Add( _coAuthorID );
    if ( !_author._AuthorAttributes._Venues.Contains( _venueID ) )
        _author._AuthorAttributes._Venues.Add( _venueID );
}
_myReader_1.Close();  

按照显示的数据结构和图像中的描述,似乎所有属性(Paper、CoAuthor、Venue)都是列表类型,因此不需要将属性声明为List<AuthorAttributes>。按照此操作您想要实现的目标:

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 AuthorAttributes()
      };
   }

   // Check if list _PaperID doesn't exist
   if (_author._Attributes._PaperID == null)
   {
      // Create new _PaperID
      _author._Attributes._PaperID = new List<int>();
      // Add Paper_ID to _PaperID
      _author._Attributes._PaperID.Add(_myReader_1.GetInt32(2));
   }
   else // Add Paper_ID to existing _PaperID list
   _author._Attributes._PaperID.Add(_myReader_1.GetInt32(2));

   // Check if list _CoAuthorID doesn't exist
   if (_author._Attributes._CoAuthorID == null)    
   {
      // Create new _CoAuthorID
      _author._Attributes._CoAuthorID = new List<int>();
      // Add CoAuthor_ID to _CoAuthorID
      _author._Attributes._CoAuthorID.Add(_myReader_1.GetInt32(3));
   }
   else // Add CoAuthor_ID to existing _CoAuthorID list
   _author._Attributes._CoAuthorID.Add(_myReader_1.GetInt32(3));

   // Check if list _CoAuthorID doesn't exist
   if (_author._Attributes._VenueID == null)    
   {
      // Create new _VenueID
      _author._Attributes._VenueID = new List<int>();
      // Add Venue_ID to _VenueID
      _author._Attributes._VenueID.Add(_myReader_1.GetInt32(4));
   }
   else // Add Venue_ID to existing _VenueID list
   _author._Attributes._VenueID.Add(_myReader_1.GetInt32(4));

   // Add Year to _Year
   _author._Attributes._Year =_myReader_1.GetInt32(5);

   if (!_eAthors.Contains(_author))
   _eAthors.Add(_author);
}
_myReader_1.Close();

author == null 检查中初始化后立即添加新创建的作者。然后检查是否 author.PaperID == null,如果是,则添加 AuthorAttributes。像这样:

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>()
      };  

      _eAthors.Add(_author); // ********** Add the new author
   }  

   // Watch out!!! author.Attributes may be null for existing authors!!!
   if (author.Attributes.PaperID == null || author.PaperID.Count == 0) // Check for PaperID existence
   {
       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);
   }
}
_myReader_1.Close();  

当然,如有必要,您可以单独处理每个属性,方法是为每个属性添加一个 if 块。