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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

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

    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<OptionPape rOptions { 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.Po llPaper = (from p in database.Polls
    join o in database.Option s 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<OptionPape r{
    Option = ??????
    Votes = ?????
    }.ToList(),
    OptionsCSV = string.Join(", ",
    pog.Select(o =o.Answer).ToAr ray())
    }).SingleOrDefa ult();

    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

  • shapper

    #2
    Re: Please, need help to finish a query. Thank You.

    On Sep 30, 1:33 am, shapper <mdmo...@gmail. comwrote:
    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<OptionPape rOptions { 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.Po llPaper = (from p in database.Polls
                                    join o indatabase.Opti ons on p.PollID
    equals o.PollID
                                    join v indatabase.Vote s 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<OptionPape r{
                                        Option = ??????
                                        Votes = ?????
                                      }.ToList(),
                                      OptionsCSV = string.Join(", ",
    pog.Select(o =o.Answer).ToAr ray())
                                    }).SingleOrDefa ult();
    >
    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

    Comment

    • Family Tree Mike

      #3
      Re: Please, need help to finish a query. Thank You.

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


      "shapper" <mdmoura@gmail. comwrote in message
      news:89680d6a-f8c6-4e53-bf3a-5e6c0543a2ab@l4 2g2000hsc.googl egroups.com...
      On Sep 30, 1:33 am, shapper <mdmo...@gmail. comwrote:
      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<OptionPape rOptions { 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.Po llPaper = (from p in database.Polls
      join o in database.Option s 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<OptionPape r{
      Option = ??????
      Votes = ?????
      }.ToList(),
      OptionsCSV = string.Join(", ",
      pog.Select(o =o.Answer).ToAr ray())
      }).SingleOrDefa ult();
      >
      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

      Comment

      • shapper

        #4
        Re: Please, need help to finish a query. Thank You.

        On Oct 1, 3:02 am, "Family Tree Mike"
        <FamilyTreeM... @ThisOldHouse.c omwrote:
        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);
           }
        >
        "shapper" <mdmo...@gmail. comwrote in message
        >
        news:89680d6a-f8c6-4e53-bf3a-5e6c0543a2ab@l4 2g2000hsc.googl egroups.com...
        On Sep 30, 1:33 am, shapper <mdmo...@gmail. comwrote:
        >
        >
        >
        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<OptionPape rOptions { 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.Po llPaper = (from p in database.Polls
        join o in database.Option s 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<OptionPape r{
        Option = ??????
        Votes = ?????
        }.ToList(),
        OptionsCSV = string.Join(", ",
        pog.Select(o =o.Answer).ToAr ray())
        }).SingleOrDefa ult();
        >
        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
        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.Selec t(op =op.Answer).ToA rray()),
        Options = (from o in p.Options
        select new OptionPaper() {
        Option = o,
        Votes = o.Votes.Count
        }).ToList()
        }).SingleOrDefa ult();

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

        Thanks,
        Miguel

        Comment

        Working...