【问题标题】:Reading into a Dictionary from SqlDataReader从 SqlDataReader 读入字典
【发布时间】:2016-06-07 19:59:27
【问题描述】:

我在一个类中声明了一个字典:

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 Server 读取数据时:

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 读入字典键和值

【问题讨论】:

  • 不管你的sql响应中有什么数据,你都需要使用'_Paper = new Dictionary();'在尝试添加任何内容之前。
  • 字典不能与属性 getter/setter 一起使用。 stackoverflow.com/a/11583686/4846465
  • 在不知道 SQL 查询或它返回的一些示例数据的情况下,这有点难以回答。
  • _添加 _underscores _to _everything _不管 _of _visibility _is _a _peculiar _style _Ive _never _seen _anywhere _else。它当然不会提高可读性。然而,更糟糕的是使用魔法常量来读取列,而不是使用.GetOrd() 来确定它们的位置。面对不断变化的结果集,这段代码是不必要的脆弱(而且维护者也很难验证它是否正确)。
  • @Dirk 我添加了一个使用 Sql 查询的更新

标签: c# dictionary sqldatareader


【解决方案1】:

这可以使用索引器工作。取自:https://stackoverflow.com/a/11583759/4846465

更新模型:

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

【讨论】:

  • @Ryan--我将如何在using() {while(){ // Here I have to use it} } 中使用它?
  • 会是var attrb = new List&lt;AuthorAttributes&gt;{};吗?
  • 我已经添加它来代替_author._Attributes.Add(new AuthorAttributes {...});
  • 抱歉回复晚了。是的,你完全正确。使用上面的代码 sn-p 代替:_author._Attributes.Add(new AuthorAttributes {...});。有趣的练习,我学到了新东西。
  • 这很激励@Ryan :)
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 2021-09-20
  • 2018-03-19
  • 1970-01-01
  • 2018-08-27
  • 2013-02-13
相关资源
最近更新 更多