winforms - Create Poll in C# (windows form) -


i want create poll. have table questions(fields: id,question) , answers(fields: id,questionid,answer). there table results(fields: questionid,answerid,userid).i want show percent of responding item in datagridview. example when enter question id, datagridview show:

choose question id:1

option...........percent

1 ------------------------- 30

2 -------------------------- 20

3---------------------------50

4-------------------------- 10

this code result not show it:

    int = convert.toint32(textbox1.text);          var q = (from s in session.db.poolusers                  s.poolqid ==                  select s);         var qq = (from c in q                   group c c.poolaid agroups                   select agroups.key);         var qqq = (from c in qq                    select c).count();         messagebox.show(qqq.tostring()); 

and there classes:

    public partial class poola    //for answers {     public int id { get; set; }     public string answer { get; set; }     public string questionid { get; set; } }       public partial class poolq    //questions {     public int id { get; set; }     public string question { get; set; }     public string answerid { get; set; }     public string status { get; set; }     public string startdate { get; set; }     public string enddate { get; set; } }       public partial class pooluser  //resaults {     public int id { get; set; }     public int poolqid { get; set; }     public int poolaid { get; set; }     public int userid { get; set; } } 

this not optimal way it, highly consider you, change entities.

here solution:

int questionid = 1; var questionanswers = list.where(elem => elem.poolqid == questionid); int questionanswerscount = questionanswers.count(); var answersprecentage = questionanswers                         .groupby(elem => elem.poolaid)                         .todictionary(grp => grp.key, grp => (grp.count() * 100.0 / questionanswerscount)); 

.net fiddle link


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -