为什么我的List对象不包含“添加"的定义? [英] Why doesn't my List object contain a definition for 'Add'?

查看:64
本文介绍了为什么我的List对象不包含“添加"的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将SQL Server中的数据集放入列表中,以便随后将其显示在页面上.老实说,我在这里看了很多答案,每一次不同的尝试都会使我遇到不同的错误.

I'm currently trying to get a dataset from SQL server into a list so I can then display this on a page. I've honestly looked at a lot of answers here, and every different attempt brings me to a different error.

根据我的阅读,该应该有效. ScriptList List ,并且应具有 Add 功能.但是,事实并非如此.这是我目前的代码:

From what I've read, this should work. ScriptList is a List and should have the Add functionality. However, it simply doesn't. This is my code at the moment:

    public ScriptList Index()
    {
        var scriptList = new ScriptList();

        //List<ScriptItem> scriptlst = new List<ScriptItem>();

        string connectionString = _configuration["ConnectionString:localhost"];

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string statement = "SELECT ScriptGUID, ScriptName, ScriptDescription, DateAdded FROM dbo.Scropts;";
            SqlCommand command = new SqlCommand(statement, connection);
            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while(reader.Read())
                {
                    scriptList.Add<ScriptItem>(new ScriptItem{GUID = Convert.ToString(reader["ScriptGUID"]),
                                                              Name = Convert.ToString(reader["ScriptName"]),
                                                              ShortDescription = Convert.ToString(reader["ScriptDescription"]),
                                                              DateAdded = Convert.ToDateTime(reader["DateAdded"])});

                }
            }


        }

        return scriptList;
    }

}
public class ScriptList
{
    public List<ScriptItem> ScriptItems {get;set;}
}
public class ScriptItem
{
    public string GUID {get;set;}
    public string Name {get;set;}
    public string ShortDescription {get;set;}
    public DateTime DateAdded {get;set;}
}

这给了我下面的错误:

'ScriptList'不包含'Add'的定义,最佳扩展方法重载'ConfigurationExtensions.Add(IConfigurationBuilder,Action)'需要类型为'IConfigurationBuilder'的接收器

'ScriptList' does not contain a definition for 'Add' and the best extension method overload 'ConfigurationExtensions.Add(IConfigurationBuilder, Action)' requires a receiver of type 'IConfigurationBuilder'

我尝试将 ScriptList 的定义更改为 List< ScriptItem>scriptlist = new List< ScriptItem>(); ,但这会导致以下错误:

I've tried change the definition of ScriptList to List<ScriptItem> scriptlist = new List<ScriptItem>(); however, this then results in the error below:

无法将类型'System.Collections.Generic.List'隐式转换为'asp.Pages.ScriptList'

Cannot implicitly convert type 'System.Collections.Generic.List' to 'asp.Pages.ScriptList'

我真的不明白这里出了什么问题.为什么 scriptList 不具有 Add 功能,为什么 scriptlist 没有 尽管它们都被声明为(显然)不相同的 List< ScriptItem> ?

I really don't understand what's wrong here. Why does scriptList not have the Add functionality, and why is it that scriptlist does yet despite that both are declared as List<ScriptItem> that aren't (apparently) the same?

推荐答案

否,您创建的类具有 List< T> 成员.这并不意味着您的类继承了该成员的所有成员.

No, you have created a class that has a List<T> member. That doesn't mean that your class inherits all members from said member.

您只是在寻找 scriptList.ScriptItems.Add(),请注意添加的 ScriptItems .是您的列表,而不是您的 ScriptList 类.

You're simply looking for scriptList.ScriptItems.Add(), note the added ScriptItems. That is your list, not your ScriptList class.

这篇关于为什么我的List对象不包含“添加"的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆