简体   繁体   English

如何在C#中将对象添加到列表项

[英]How to add object to list item in c#

I have following classes: 我有以下课程:

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

I have QualifyResponse object which is populated with ProviderQualifyResponse but QuestionGroupList is empty. 我有QualifyResponse对象,其中已填充ProviderQualifyResponseQuestionGroupList为空。 Now I want to fill QuestionGroupList . 现在,我要填充QuestionGroupList When I try to do it like this: 当我尝试这样做时:

QualifyResponse qualifyResponse = response;
qualifyResponse.ProviderList.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();

I get error: 我得到错误:

System.Collections.Generic.List' does not contain a definition for 'QuestionGroupList' and no extension method 'QuestionGroupList' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?) System.Collections.Generic.List'不包含'QuestionGroupList'的定义,找不到扩展方法'QuestionGroupList'接受类型为'System.Collections.Generic.List'的第一个参数(是否缺少using指令或组装参考?)

How can I add a List<QuestionGroup> to my qualifyResponse.ProviderList ? 如何将List<QuestionGroup>添加到我的qualifyResponse.ProviderList

The error is this expression: 错误是此表达式:

qualifyResponse.ProviderList.QuestionGroupList

ProviderList is of type List. ProviderList的类型为List。 You'll have to change it so you populate the correct item. 您必须对其进行更改,以便填充正确的项目。 Something like this: 像这样:

int index = ...;
qualifyResponse.ProviderList[index].QuestionGroupList = ...

Problem is that QuestionGroupList is property of class ProviderQualifyResponse and if you want to add List, you need to assign it to property of object. 问题是QuestionGroupList是ProviderQualifyResponse类的属性,如果要添加List,则需要将其分配给object的属性。 Example how to do that for all providers: 示例如何为所有提供程序执行此操作:

QualifyResponse qualifyResponse = response;
foreach(var provider in qualifyResponse.ProviderList)
{
     provider.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
}

Given that qualifyResponse.ProviderList is of type List<ProviderQualifyResponse> , you are trying to access List.QuestionGroupList , which doesn't exist as the error states. 鉴于qualifyResponse.ProviderList的类型为List<ProviderQualifyResponse> ,您正在尝试访问List.QuestionGroupList ,由于错误状态该列表不存在。

You'll either need to iterate through the instances in the list to access the instance properties, if that's your intention, or select an instance from the list you wish to instantiate. 如果您要这样做,则需要遍历列表中的实例以访问实例属性,或者从列表中选择要实例化的实例。

QualifyResponse qualifyResponse = response;
foreach (var providerQualifyResponse in qualifyResponse.ProviderList)
{
    providerQualifyResponse.QuestionGroupList = new List<DataTypes.BuyFlow.Entities.QuestionGroup>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM