繁体   English   中英

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

[英]group and add list of items of object C#

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

我的班级等级:

public class QuestionarieDisplay
{
    public QuestionarieDisplay()
    {
        QuestionTypeList = new List<QuestionTypeList>();
    }
    public Nullable<int> QuestionnaireId { get; set; }
    public Nullable<int> QuestionnaireGroupId { get; set; }
    public string QuestionnaireGroup { get; set; }
    public List<QuestionTypeList> QuestionTypeList { get; set; }
}

public class QuestionTypeList
{
    public QuestionTypeList()
    {
         QuestionList= new List<QuestionList>();
    } 
    public Nullable<int> QuestionTypeId { get; set; }
    public string QuestionType { get; set; }
    public List<QuestionList> QuestionList{ get; set; }  
}

public class QuestionList
{
   public QuestionList()
    {
        Answer= new List<Answer>();
    }
    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; }
}

public class Answer
{
    public string Answers { get; set; }
}

存储过程的JSON结果:

{
    "QuestionnaireId": 2,
    "QuestionnaireGroupId": 1,
    "QuestionnaireGroup": "Please rank based on the following areas",
    "QuestionId": 2,
    "Question": "Full Name?",
    "QuestionTypeId": 3,
    "QuestionType": "Short Text",
    "ScaleId": null,
    "ScaleType": null,
    "Answers": null,
    "Attribute": "Required",
    "AttributeValue": "Yes"
}, 

{
    "QuestionnaireId": 2,
    "QuestionnaireGroupId": 1,
    "QuestionnaireGroup": "Please rank based on the following areas",
    "QuestionId": 5,
    "Question": "Experience?",
    "QuestionTypeId": 9,
    "QuestionType": "Dropdown",
    "ScaleId": null,
    "ScaleType": null,
    "Answers": null,
    "Attribute": null,
    "AttributeValue": null
},

{
    "QuestionnaireId": 2,
    "QuestionnaireGroupId": 1,
    "QuestionnaireGroup": "Please rank based on the following areas",
    "QuestionId": 7,
    "Question": "Email Adddress?",
    "QuestionTypeId": 3,
    "QuestionType": "Short Text",
    "ScaleId": null,
    "ScaleType": null,
    "Answers": null,
    "Attribute": null,
    "AttributeValue": null
},

{
    "QuestionnaireId": 2,
    "QuestionnaireGroupId": 1,
    "QuestionnaireGroup": "Please rank based on the following areas",
    "QuestionId": 12,
    "Question": "Your comments?",
    "QuestionTypeId": 4,
    "QuestionType": "Long Text",
    "ScaleId": null,
    "ScaleType": null,
    "Answers": null,
    "Attribute": null,
    "AttributeValue": null
},

我把它划分为:

var result = result.GroupBy(u => u.QuestionTypeId).Select(grp => 
grp.ToList()).ToList();

但我无法将其添加到列表中。

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

假设你有类似的东西

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

你可以使用:

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();

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

public List<QuestionList> QuestionList{ get; set; }

一个选项可能是更改两个类来实现一个列表,使用这种方法,你将能够覆盖列表方法,如'add'和'exists',将所有逻辑放入你需要添加一个新项目到名单。

暂无
暂无

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

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