C# Store returned sql data to be output as table -
users fill out form. each saved form has unique id. each field in form has unique id. each response has unique id. example returned data (edit: sql query):
form id form field id response id response 1 1 1 response q1. 1 2 2 response q2. ... 2 1 10 2nd response q1. 2 2 11 2nd response q2. ... 3 1 20 3rd response q1. 3 2 21 3rd response q2.
when data output, needs this:
q1 q2 ... qn response q1 response q2 ... response qn 2nd response q1. 2nd response q2. ... 2nd response qn 3rd response q1. 3rd response q2. ... 3rd response qn
i've tried looking @ using dictionary:
var array_report_data = new dictionary<string, dictionary<string, dictionary<string, string>>>();
and adding data this:
array_report_data[form_id][form_field_id]["response_id"] = 1; array_report_data[form_id][form_field_id]["response"] = "response q1.";
but can't loop through way in php. i'm trying learn proper way in c#, have no clue start. believe biggest problem how i'm trying store data (in dictionary), seems incorrect way this.
i should mention final table being generated onclick event, i'm using jquery make ajax call , doing heavy lifting in asmx page.
edit: how original data retrieved:
select [qw_forms_saved_id], [qw_form_fields_id], [qw_forms_saved_fields_id], [qw_form_field_response] [qw_forms_saved] [eff_end_dt] null , [eff_end_dt] null order [qw_forms_saved_id] asc, [qw_form_fields_id] asc
this sample based on test data should started.
namespace consoleapplication3 { using system; using system.collections.generic; using system.linq; internal class program { #region methods private static void main(string[] args) { #region data var data = new list<formdata> { new formdata { formid = 1, formfieldid = 1, responseid = 1, response = "response q1." }, new formdata { formid = 1, formfieldid = 2, responseid = 2, response = "response q2." }, new formdata { formid = 2, formfieldid = 1, responseid = 10, response = "2nd response q1." }, new formdata { formid = 2, formfieldid = 2, responseid = 11, response = "2nd response q2." }, new formdata { formid = 3, formfieldid = 1, responseid = 20, response = "3rd response q1." }, new formdata { formid = 3, formfieldid = 2, responseid = 21, response = "3rd response q2." }, }; #endregion var groups = data.groupby(g => g.formfieldid).select( s => new { s.key, answers = s.select(s2 => s2.response).tolist() }).tolist(); console.writeline(string.concat(groups.select(g => "q" + g.key.tostring("###").padright(29)))); int maxrows = groups.max(m => m.answers.count); (int = 0; < maxrows; i++) { console.writeline(string.concat(groups.select(s1 => (i < s1.answers.count ? s1.answers[i] : string.empty).padright(30)))); } } #endregion private class formdata { #region public properties public int formfieldid { get; set; } public int formid { get; set; } public string response { get; set; } public int responseid { get; set; } #endregion } } }
output:
q1 q2 response q1. response q2. 2nd response q1. 2nd response q2. 3rd response q1. 3rd response q2.
cheers
Comments
Post a Comment