2016-06-12 60 views
-1

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

enter image description here

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

enter image description here

+0

这有严重的问题。您如何知道CoAuthor_ID 1695352与哪个论文相关联?或者当年移至2007年时。您需要更正式的数据结构。 – Paparazzi

+0

我需要跟踪作者及其属性,即相应年份中的纸张,作者和地点,这里不需要CoAuthor_ID 1695352与 – maliks

+0

相关联的文章那么您甚至在这里没有*相应*年。为什么新作者没有初始化这些列表?看起来很sl to。 – Paparazzi

回答

1

根据显示的数据结构和看到图像中描绘的内容,似乎所有的att ributes(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(); 
+1

尽管每个属性都单独处理,但它看起来更漂亮! – maliks

+0

你不需要重复所有这些。添加。 – Paparazzi

+0

那么如何分别在每个属性列表中添加? – maliks

1

假设你的数据结构,其余部分是这样的:

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(); 
+0

'VenueID'也是列表,我没有'Paper'类在这里,而是我有'AuthorAttributes'类 – maliks

+0

好吧,将编辑以反映那 –

+1

还有一件事! 'PaperID'也是一个列表,因为'Author_ID'有很多'Paper_ID' – maliks

-1

添加新创建笔者在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块每一个。