【问题标题】:How to fill a custom typed List<> from SQL Server如何从 SQL Server 填充自定义类型的 List<>
【发布时间】:2016-05-09 11:43:13
【问题描述】:

我有一个名为Author.cs 的类定义为:

public class Author : Interfaces.INode
{
    private List<INode> _targetList;
    private List<IAttribute> _attributeObject;

    // Author class Constructor
    public Author()
    {
        _targetList = new List<INode>();
    }
    //implementazion of _TargetObject INode method
    public List<INode> _TargetObject
    {
        get
        {
             return _targetList;
        }
    }
    //implementazion of _AttributeObject INode method
    public List<IAttribute> _AttributeObject
    {
        get
        {
             return _attributeObject;
        }
    }

    public int _aID { get; set; }
    public string _aName { get; set; }  
    // 'CoAuthor', 'Venue' and 'Paper' are classes that  
    // implements an interface i.e. `IAttribute`
    public List<CoAuthor> _aCoAuthors { get; set; } 
    public List<Venue> _aVenue { get; set; }
    public List<Paper> _aPapers { get; set; }
    public string _aArea { get; set; }
}  

它在Interfaces文件夹中实现了一个名为Interfaces.INode.cs的接口,定义为:

public interface INode
{
    List<INode> _TargetObject { get; }
    List<IAttribute> _AttributeObject { get; }
}

public interface IAttribute : INode
{}  

现在我想填写一个列表,即List&lt;Author&gt;,即在另一个名为AuthorCollector.cs的类中

List<Author> _eAthors = new List<Author>();  

我尝试过:

try
{
    SqlCommand _myCommand_1 = _con.CreateCommand();
    _myCommand_1.CommandText = @"SELECT Author_ID FROM M_DataFull  
                                 ORDER BY Author_ID, Year";
    var _AuthID = 0;
    int _Row_Counter = 0;

    using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
    {
        while (_myReader_1.Read())
        {
            _Row_Counter++;
            _eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]));
        }
        _myReader_1.Close();
     }
}
catch(Exception e)
{
     Console.WriteLine(e.Message);
}  

错误是:

_eAuthors.Add() 的最佳重载方法匹配有一些无效参数。

【问题讨论】:

  • 您尝试将 int 添加到作者列表中。伪代码:new Author(Convert.ToInt32(_myReader_1["Author_ID"]));
  • @Mat 同样我问过如何填写“列表”,即自定义类型列表
  • 尝试使用Entity Framework 更灵活
  • 您有一个 List 但您要添加一个整数 _eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]))。您首先需要创建一个新作者:Author newAuthor = new Author();。然后将作者添加到列表中:_eAthors.Add(newAuthor);。然后设置作者id:newAuthor._aID = Convert.ToInt32(myReader_1["Author_ID"]);

标签: c# sql-server list class user-defined


【解决方案1】:
using (SqlDataReader _myReader_1 = _myCommand_1.ExecuteReader())
{
    while (_myReader_1.Read())
    {
       _Row_Counter++;
       Author author = new Author();
       author._aId = Convert.ToInt32(_myReader_1["Author_ID"]);
       author._aName = Convert.ToString(_myReader_1["Author_Name"]);
       //etc...
       _eAthors.Add(author);
    }
    _myReader_1.Close();
}

【讨论】:

  • @Nino--你已经优雅地添加了一个作者到列表中,那么更多作者要添加什么呢?
  • 代码在while循环内...所以每个作者都被添加到列表中。它适用于零个、一个或多个作者。
  • @Nino--对于Author 类的其他属性,它们本身也是自定义类型列表,如公共List&lt;CoAuthor&gt;List&lt;Venue&gt;List&lt;Paper&gt; 如何填充这些?
  • 您必须像作者一样手动填写它们。您可以更改查询以将作者连接到其他表,也可以使用多个数据库查询一一填充属性。
  • 我可以在这里使用 EF 吗?因为我的所有数据都在 SQL Server 表中
【解决方案2】:

您尝试将 int 添加到作者列表中。伪代码:

_eAthors.Add(new Author(Convert.ToInt32(_myReader_1["Author_ID"])));

    _eAthors.Add(new Author(){_aID =Convert.ToInt32(_myReader_1["Author_ID"]}));

无论如何,我会为此使用 ORM 框架,例如 NHibernate 或 EntityFramework。比自己做所有的 SQL 映射要容易得多...

【讨论】:

  • @Mat--这仍然不是解决方案,因为我们在 Author 类中没有任何构造函数需要 1 个参数
  • 我已经添加了一段有效的代码,所以你不必编写构造函数:-)
  • @Mat--您建议使用 EntityFramework,但我对此还不是很熟悉
  • 这就是我提出这个建议的原因...... ORM 可以为您完成这项工作,因此您不必编写数据库中对象的具体化代码
【解决方案3】:

_eAthorsAuthors 的集合,但代码试图添加一个 int 值,这在这种情况下会导致错误。

修改这一行

_eAthors.Add(Convert.ToInt32(_myReader_1["Author_ID"]))

_eAthors.Add(new Author
             {
                _aid= Convert.ToInt32(_myReader_1["Author_ID"]),  
                // add additional properties if you have one.                     
             });  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多