2016-06-12 59 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塊每一個。