Please, need help to finish a query. Thank You.

S

shapper

Hello,

I have three tables: Polls (PollId, Question), Options (OptionID,
Answer) and Votes (VoteID, OptionID)

I then created two Wrapper Classes:

PostPaper with the following properties:
public Poll Poll { get; set; }
public List<OptionPaper> Options { get; set; }
public string OptionsCSV { get; set; }

OptionPaper with the following properties:
public Option Option { get; set; }
public int Votes { get; set; }

I need, given an PollId, to get fill a PostPaper with all its options
and for each option count the votes:

pollViewData.PollPaper = (from p in database.Polls
join o in database.Options on p.PollID
equals o.PollID
join v in database.Votes on o.OptionID
equals v.OptionID
where p.PollID == id
group o by p into pog
select new PollPaper {
Poll =
pog.Key,
Options = new List<OptionPaper> {
Option = ??????
Votes = ?????
}.ToList(),
OptionsCSV = string.Join(", ",
pog.Select(o => o.Answer).ToArray())
}).SingleOrDefault();

I am having problems in creating the Option and Count the votes of
each OptionPaper in List Options.

Could someone, please, help me out?

Thanks,
Miguel
 
S

shapper

Hello,

I have three tables: Polls (PollId, Question), Options (OptionID,
Answer) and Votes (VoteID, OptionID)

I then created two Wrapper Classes:

   PostPaper with the following properties:
      public Poll Poll { get; set; }
      public List<OptionPaper> Options { get; set; }
      public string OptionsCSV { get; set; }

   OptionPaper with the following properties:
      public Option Option { get; set; }
      public int Votes { get; set; }

I need, given an PollId, to get fill a PostPaper with all its options
and for each option count the votes:

      pollViewData.PollPaper = (from p in database.Polls
                                join o indatabase.Options on p.PollID
equals o.PollID
                                join v indatabase.Votes on o.OptionID
equals v.OptionID
                                where p.PollID == id
                                group o by p into pog
                                select new PollPaper {
                                  Poll =
pog.Key,
                                  Options = new List<OptionPaper> {
                                    Option = ??????
                                    Votes = ?????
                                  }.ToList(),
                                  OptionsCSV = string.Join(", ",
pog.Select(o => o.Answer).ToArray())
                                }).SingleOrDefault();

I am having problems in creating the Option and Count the votes of
each OptionPaper in List Options.

Could someone, please, help me out?

Thanks,
Miguel

Please, anyone? I have been trying to make this work but until now I
wasn't able.

Thanks,
Miguel
 
F

Family Tree Mike

I've come up with something like this:

PostPaper pp = new PostPaper();
pp.Poll = (from p in Polls where p.PollID == pollID select p).First();
var opts = (from o in Options where o.PollID == pollID select o);

OptionPaper op;
foreach (Option o in Options)
{
op = new OptionPaper();
op.Option = o;
op.Votes = (from v in Votes where v.OptionID == o.OptionID select
v).Count();
pp.Options.Add(op);
}


Hello,

I have three tables: Polls (PollId, Question), Options (OptionID,
Answer) and Votes (VoteID, OptionID)

I then created two Wrapper Classes:

PostPaper with the following properties:
public Poll Poll { get; set; }
public List<OptionPaper> Options { get; set; }
public string OptionsCSV { get; set; }

OptionPaper with the following properties:
public Option Option { get; set; }
public int Votes { get; set; }

I need, given an PollId, to get fill a PostPaper with all its options
and for each option count the votes:

pollViewData.PollPaper = (from p in database.Polls
join o in database.Options on p.PollID
equals o.PollID
join v in database.Votes on o.OptionID
equals v.OptionID
where p.PollID == id
group o by p into pog
select new PollPaper {
Poll =
pog.Key,
Options = new List<OptionPaper> {
Option = ??????
Votes = ?????
}.ToList(),
OptionsCSV = string.Join(", ",
pog.Select(o => o.Answer).ToArray())
}).SingleOrDefault();

I am having problems in creating the Option and Count the votes of
each OptionPaper in List Options.

Could someone, please, help me out?

Thanks,
Miguel

Please, anyone? I have been trying to make this work but until now I
wasn't able.

Thanks,
Miguel
 
S

shapper

I've come up with something like this:

   PostPaper pp = new PostPaper();
   pp.Poll = (from p in Polls where p.PollID == pollID select p).First();
   var opts = (from o in Options where o.PollID == pollID select o);

   OptionPaper op;
   foreach (Option o in Options)
   {
    op = new OptionPaper();
    op.Option = o;
    op.Votes = (from v in Votes where v.OptionID == o.OptionID select
v).Count();
    pp.Options.Add(op);
   }













Please, anyone? I have been trying to make this work but until now I
wasn't able.

Thanks,
Miguel

I am using the following:

PollPaper paper = (from p in database.Polls
where p.PollID == id
select new PollPaper {
Poll = p,
OptionsCSV = string.Join(",",
p.Options.Select(op => op.Answer).ToArray()),
Options = (from o in p.Options
select new OptionPaper() {
Option = o,
Votes = o.Votes.Count
}).ToList()
}).SingleOrDefault();

In think it is ok ... but could someone give me some feedback?

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top