从 SqlDataReader 读入字典
Reading into a Dictionary from SqlDataReader
我在 class 中将字典声明为:
public class AuthorAttributes
{
//public int _Paper_ID { get; set; }
public Dictionary<Int32, Int32> _Paper = new Dictionary<Int32, Int32>();
public int _CoAuthor_ID { get; set; }
public int _Venue_ID { get; set; }
}
而字典会将 _Paper_ID
和 _Paper_Category
类型 Int32
存储为 _Paper
。
从 SQL 服务器读取数据时:
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
while (_myReader_1.Read())
{
int _authorID = _myReader_1.GetInt32(0);
Author _author = _eAthors.FirstOrDefault(_a => _a._Author_ID == _authorID);
if (_author == null)
{
_author = new Author {
_Author_ID = _authorID,
_Author_Name = _myReader_1.GetString(1),
_Year = _myReader_1.GetInt32(6),
_Attributes = new List<AuthorAttributes>()
};
_eAthors.Add(_author);
}
_author._Attributes.Add(new AuthorAttributes {
_Paper = // How to read into Dictionary "_Paper"
_CoAuthor_ID = _myReader_1.GetInt32(4),
_Venue_ID = _myReader_1.GetInt32(5)
}
);
}
_myReader_1.Close();
}
更新
使用的 Sql 查询如下:
_myCommand_1.CommandText = @"SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID,
P.Paper_Category, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM AuthorCoAuthor AC
JOIN AuthorPaper AP ON AP.Author_ID = AC.Author_ID AND
AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
JOIN Paper P ON P.Paper_ID = AP.Paper_ID
ORDER BY
AC.Author_ID, AC.Year, AC.Paper_ID,
AC.CoAuthor_ID, AP.Venue_ID";
我将如何使用 SqLDataReader
读入字典键和值
这可以使用索引器工作。摘自:
更新模型:
public class AuthorAttributes
{
private readonly Dictionary<Int32, Int32> _paper = new Dictionary<Int32, Int32>();
public Int32 this[Int32 key]
{
// returns value if exists
get { return _paper[key]; }
// updates if exists, adds if doesn't exist
set { _paper[key] = value; }
}
public int _CoAuthor_ID { get; set; }
public int _Venue_ID { get; set; }
}
然后实施(假设_Paper_ID和_Paper_Category来自哪里):
var attrb = new AuthorAttributes
{
_CoAuthor_ID = _myReader_1.GetInt32(4),
_Venue_ID = _myReader_1.GetInt32(5)
}
// Assumes _myReader_1.GetInt32(3) is _Paper_ID
// Assumes _myReader_1.GetInt32(2) is _Paper_Category
attrb[_myReader_1.GetInt32(3)] = _myReader_1.GetInt32(2);
_author._Attributes.Add(attrb);
我在 class 中将字典声明为:
public class AuthorAttributes
{
//public int _Paper_ID { get; set; }
public Dictionary<Int32, Int32> _Paper = new Dictionary<Int32, Int32>();
public int _CoAuthor_ID { get; set; }
public int _Venue_ID { get; set; }
}
而字典会将 _Paper_ID
和 _Paper_Category
类型 Int32
存储为 _Paper
。
从 SQL 服务器读取数据时:
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
while (_myReader_1.Read())
{
int _authorID = _myReader_1.GetInt32(0);
Author _author = _eAthors.FirstOrDefault(_a => _a._Author_ID == _authorID);
if (_author == null)
{
_author = new Author {
_Author_ID = _authorID,
_Author_Name = _myReader_1.GetString(1),
_Year = _myReader_1.GetInt32(6),
_Attributes = new List<AuthorAttributes>()
};
_eAthors.Add(_author);
}
_author._Attributes.Add(new AuthorAttributes {
_Paper = // How to read into Dictionary "_Paper"
_CoAuthor_ID = _myReader_1.GetInt32(4),
_Venue_ID = _myReader_1.GetInt32(5)
}
);
}
_myReader_1.Close();
}
更新
使用的 Sql 查询如下:
_myCommand_1.CommandText = @"SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID,
P.Paper_Category, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM AuthorCoAuthor AC
JOIN AuthorPaper AP ON AP.Author_ID = AC.Author_ID AND
AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
JOIN Paper P ON P.Paper_ID = AP.Paper_ID
ORDER BY
AC.Author_ID, AC.Year, AC.Paper_ID,
AC.CoAuthor_ID, AP.Venue_ID";
我将如何使用 SqLDataReader
这可以使用索引器工作。摘自:
更新模型:
public class AuthorAttributes
{
private readonly Dictionary<Int32, Int32> _paper = new Dictionary<Int32, Int32>();
public Int32 this[Int32 key]
{
// returns value if exists
get { return _paper[key]; }
// updates if exists, adds if doesn't exist
set { _paper[key] = value; }
}
public int _CoAuthor_ID { get; set; }
public int _Venue_ID { get; set; }
}
然后实施(假设_Paper_ID和_Paper_Category来自哪里):
var attrb = new AuthorAttributes
{
_CoAuthor_ID = _myReader_1.GetInt32(4),
_Venue_ID = _myReader_1.GetInt32(5)
}
// Assumes _myReader_1.GetInt32(3) is _Paper_ID
// Assumes _myReader_1.GetInt32(2) is _Paper_Category
attrb[_myReader_1.GetInt32(3)] = _myReader_1.GetInt32(2);
_author._Attributes.Add(attrb);