C# 如何在c中向列表项添加对象
我有以下课程:C# 如何在c中向列表项添加对象,c#,linq,list,object,C#,Linq,List,Object,我有以下课程: public class QualifyResponse { List<ProviderQualifyResponse> _providerList = new List<ProviderQualifyResponse>(); public List<ProviderQualifyResponse> ProviderList { get { return _providerList; }
public class QualifyResponse
{
List<ProviderQualifyResponse> _providerList = new List<ProviderQualifyResponse>();
public List<ProviderQualifyResponse> ProviderList
{
get { return _providerList; }
set { _providerList = value; }
}
}
public class ProviderQualifyResponse
{
private string _providerCode = string.Empty;
public string ProviderCode
{
get { return _providerCode; }
set { _providerCode = value; }
}
private List<QuestionGroup> _questionGroupList = new List<QuestionGroup>();
public List<QuestionGroup> QuestionGroupList
{
get { return _questionGroupList; }
set { _questionGroupList = value; }
}
}
我得到一个错误:
System.Collections.Generic.List“”不存在
包含“QuestionGroupList”的定义,不包含扩展方法
“QuestionGroupList”接受类型为的第一个参数
“System.Collections.Generic.List”可以是
发现是否缺少using指令或程序集引用
如何将列表添加到qualifyResponse.ProviderList中?错误是以下表达式:
qualifyResponse.ProviderList.QuestionGroupList
ProviderList属于List类型。您必须更改它,以便填充正确的项。大概是这样的:
QualifyResponse qualifyResponse = response;
qualifyResponse.ProviderList.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
int index = ...;
qualifyResponse.ProviderList[index].QuestionGroupList = ...
错误是此表达式:
qualifyResponse.ProviderList.QuestionGroupList
ProviderList属于List类型。您必须更改它,以便填充正确的项。大概是这样的:
QualifyResponse qualifyResponse = response;
qualifyResponse.ProviderList.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
int index = ...;
qualifyResponse.ProviderList[index].QuestionGroupList = ...
问题是QuestionGroupList是类ProviderQualifyResponse的属性,若要添加列表,需要将其分配给对象的属性。 示例如何为所有提供商执行此操作:
QualifyResponse qualifyResponse = response;
foreach(var provider in qualifyResponse.ProviderList)
{
provider.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
}
问题是QuestionGroupList是类ProviderQualifyResponse的属性,若要添加列表,需要将其分配给对象的属性。 示例如何为所有提供商执行此操作:
QualifyResponse qualifyResponse = response;
foreach(var provider in qualifyResponse.ProviderList)
{
provider.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
}
假设qualifyResponse.ProviderList的类型为List,则您正在尝试访问List.QuestionGroupList,该列表在错误状态下不存在
如果您打算访问实例属性,则需要遍历列表中的实例,或者从要实例化的列表中选择一个实例
QualifyResponse qualifyResponse = response;
foreach (var providerQualifyResponse in qualifyResponse.ProviderList)
{
providerQualifyResponse.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
}
假设qualifyResponse.ProviderList的类型为List,则您正在尝试访问List.QuestionGroupList,该列表在错误状态下不存在
如果您打算访问实例属性,则需要遍历列表中的实例,或者从要实例化的列表中选择一个实例
QualifyResponse qualifyResponse = response;
foreach (var providerQualifyResponse in qualifyResponse.ProviderList)
{
providerQualifyResponse.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
}
还要添加QuestionGroup模型类您的代码示例没有意义,QualifyResponse.QuelifyResponse?没有这样的字段或方法。\u providerList是一个空列表,您试图对该列表而不是其成员进行操作。您必须首先将项目添加到该列表中,然后处理列表中的特定元素,例如qualifyResponse.ProviderList[0]。QuestionGroupList=new list;还要添加QuestionGroup模型类您的代码示例没有意义,QualifyResponse.QuelifyResponse?没有这样的字段或方法。\u providerList是一个空列表,您试图对该列表而不是其成员进行操作。您必须首先将项目添加到该列表中,然后处理列表中的特定元素,例如qualifyResponse.ProviderList[0]。QuestionGroupList=new list;