3

Below JSON is the output from a stored procedure. I would like to generate a class which divides into group of classes based on question type questions and it has answers as a list to each question.

My class hierarchy :

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 Result from stored procedure:

{
    "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
},

I have divided it by:

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

but I am not able to add it to list.

I would like to map result in the above class hierarchy.

derHugo
  • 83,094
  • 9
  • 75
  • 115
priya_21
  • 159
  • 1
  • 2
  • 15
  • I think you need to add intermediate class: show thoses cases was use off deserialize json on c# https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp – Sanpas Jan 04 '19 at 08:06
  • This code `grp => grp.ToList()` means if i'm not mistaken result = group of list. My question is in which part of your `class hierarchy` do you want to store the `result`? – Vijunav Vastivch Jan 04 '19 at 08:17

2 Answers2

1

Assuming that you have something like

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

you could use:

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();
xdtTransform
  • 1,986
  • 14
  • 34
Piotr Wojsa
  • 938
  • 8
  • 22
0

Right now your are declaring the classes QuestionTypeList and QuestionList as generic lists like in:

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

An option could be to change both classes to implement a list, using this approach, you are going to be able to override the list methods like 'add' and 'exists' putting inside all the logic you need to add a new item to the list.

Óscar Andreu
  • 1,630
  • 13
  • 32