【发布时间】: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<Author>,即在另一个名为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