Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 分组并添加对象C的项目列表#_C# - Fatal编程技术网

C# 分组并添加对象C的项目列表#

C# 分组并添加对象C的项目列表#,c#,C#,下面是存储过程的输出。我想生成一个类,该类根据问题类型问题划分为一组类,并将答案作为每个问题的列表 我的类层次结构: 公共类问题显示 { 公共问题eDisplay() { QuestionTypeList=新列表(); } 公共可为空的QuestionnaireId{get;set;} 公共可空问题重新组合ID{get;set;} 公共字符串问题重新组合{get;set;} 公共列表问题类型列表{get;set;} } 公共类问题类型列表 { 公共问题类型列表() { QuestionList=

下面是存储过程的输出。我想生成一个类,该类根据问题类型问题划分为一组类,并将答案作为每个问题的列表

我的类层次结构:

公共类问题显示
{
公共问题eDisplay()
{
QuestionTypeList=新列表();
}
公共可为空的QuestionnaireId{get;set;}
公共可空问题重新组合ID{get;set;}
公共字符串问题重新组合{get;set;}
公共列表问题类型列表{get;set;}
}
公共类问题类型列表
{
公共问题类型列表()
{
QuestionList=新列表();
} 
公共可为空的QuestionTypeId{get;set;}
公共字符串QuestionType{get;set;}
公共列表问题列表{get;set;}
}
公开课问题单
{
公众问题清单()
{
答案=新列表();
}
公共列表答案{get;set;}
公共可为空的QuestionId{get;set;}
公共字符串问题{get;set;}
公共可为空的ScaleId{get;set;}
公共字符串ScaleType{get;set;}
公共字符串属性{get;set;}
公共字符串AttributeValue{get;set;}
}
公开课答案
{
公共字符串回答{get;set;}
}
存储过程的JSON结果:
{
“问题奈里德”:2,
“问题重新分组”:1,
“问题重组”:“请根据以下区域进行排名”,
“问题ID”:2,
“问题”:“全名?”,
“问题类型ID”:3,
“问题类型”:“短文本”,
“ScaleId”:空,
“ScaleType”:null,
“答案”:空,
“属性”:“必需”,
“属性值”:“是”
}, 
{
“问题奈里德”:2,
“问题重新分组”:1,
“问题重组”:“请根据以下区域进行排名”,
“问题ID”:5,
“问题”:“经验?”,
“问题类型ID”:9,
“问题类型”:“下拉列表”,
“ScaleId”:空,
“ScaleType”:null,
“答案”:空,
“属性”:空,
“AttributeValue”:空
},
{
“问题奈里德”:2,
“问题重新分组”:1,
“问题重组”:“请根据以下区域进行排名”,
“问题ID”:7,
“问题”:“电子邮件地址?”,
“问题类型ID”:3,
“问题类型”:“短文本”,
“ScaleId”:空,
“ScaleType”:null,
“答案”:空,
“属性”:空,
“AttributeValue”:空
},
{
“问题奈里德”:2,
“问题重新分组”:1,
“问题重组”:“请根据以下区域进行排名”,
“问题ID”:12,
“问题”:“您的意见?”,
“问题类型ID”:4,
“问题类型”:“长文本”,
“ScaleId”:空,
“ScaleType”:null,
“答案”:空,
“属性”:空,
“AttributeValue”:空
},
我将其除以:

var result=result.GroupBy(u=>u.QuestionTypeId)。选择(grp=>
grp.ToList()).ToList();
但我无法将其添加到列表中


我想将结果映射到上面的类层次结构中。

现在,您正在将类QuestionTypeList和QuestionList声明为通用列表,如:

public List<QuestionList> QuestionList{ get; set; }
公共列表问题列表{get;set;}

一个选项是更改这两个类以实现列表,使用这种方法,您将能够覆盖列表方法,如“add”和“exists”,将您需要的所有逻辑添加到列表中

public class RawDataDTO
{
    public Nullable<int> QuestionnaireId { get; set; }
    public Nullable<int> QuestionnaireGroupId { get; set; }
    public string QuestionnaireGroup { get; set; }
    public Nullable<int> QuestionTypeId { get; set; }
    public string QuestionType { get; set; }
    public List<Answer> Answer { get; set; }
    public Nullable<int> QuestionId { get; set; }
    public string Question { get; set; }
    public Nullable<int> ScaleId { get; set; }
    public string ScaleType { get; set; }
    public string Attribute { get; set; }
    public string AttributeValue { get; set; }
}
公共类RawDataDTO
{
公共可为空的QuestionnaireId{get;set;}
公共可空问题重新组合ID{get;set;}
公共字符串问题重新组合{get;set;}
公共可为空的QuestionTypeId{get;set;}
公共字符串QuestionType{get;set;}
公共列表答案{get;set;}
公共可为空的QuestionId{get;set;}
公共字符串问题{get;set;}
公共可为空的ScaleId{get;set;}
公共字符串ScaleType{get;set;}
公共字符串属性{get;set;}
公共字符串AttributeValue{get;set;}
}
您可以使用:

List<RawDataDTO> raw = // your data in raw format

List<QuestionarieDisplay> list = raw
    .GroupBy(r => new { r.QuestionnaireGroup, r.QuestionnaireGroupId, r.QuestionnaireId })
    .Select(r => new QuestionarieDisplay()
    {
        QuestionnaireId = r.Key.QuestionnaireId,
        QuestionnaireGroupId = r.Key.QuestionnaireGroupId,
        QuestionnaireGroup = r.Key.QuestionnaireGroup,
        QuestionTypeList =
            r.GroupBy(t => new { t.QuestionType, t.QuestionTypeId })
            .Select(t => new QuestionTypeList
            {
                QuestionType = t.Key.QuestionType,
                QuestionTypeId = t.Key.QuestionTypeId,
                QuestionList = t.Select(x => new QuestionList
                    {
                        QuestionId = x.QuestionId
                    })
                .ToList()
            })
            .ToList()
    }).ToList();
List raw=//原始格式的数据
列表=原始
.GroupBy(r=>new{r.QuestionnaireGroupId,r.QuestionnaireGroupId,r.QuestionnaireId})
.Select(r=>新问题显示()
{
QuestionnaireId=r.Key.QuestionnaireId,
QuestionnaireGroupId=r.Key.QuestionnaireGroupId,
QuestionnaireGroup=r.Key.QuestionnaireGroup,
问题类型表=
r、 GroupBy(t=>new{t.QuestionType,t.QuestionTypeId})
.选择(t=>new QuestionTypeList
{
QuestionType=t.Key.QuestionType,
QuestionTypeId=t.Key.QuestionTypeId,
QuestionList=t.Select(x=>newquestionlist
{
QuestionId=x.QuestionId
})
托利斯先生()
})
托利斯先生()
}).ToList();

我认为您需要添加中间类:显示这些案例是在c上反序列化json时使用的#这段代码
grp=>grp.ToList()
意味着如果我没有弄错的话,结果=一组列表。我的问题是,您希望在
类层次结构的哪一部分存储
结果