Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a ViewModel;

 public class GetQuestionViewModel
{
    public IEnumerable<Question> Questions { get; set; }
    public IEnumerable<QuestionOption> QuestionOptions { get; set; }
    public int Id { get; set; }
    public Nullable<int> Options { get; set; }
    public string QuestionOption1 { get; set; }
    public Nullable<int> QuestionOptionRanking { get; set; }
    public string Question1 { get; set; }
    public int QuestionTypeId { get; set; }
    public Nullable<int> QuestionRanking { get; set; }
    public string Answer { get; set; }
}


And I have my Query structured as thus;
[HttpGet]
       public ActionResult ViewQuestion(int page = 1)
       {
           var userId = User.Identity.GetUserId();
           ICollection<GetQuestionViewModel> question = (from q in db.Questions
               join qo in db.QuestionOptions on q.Id equals qo.QuestionId
               join r in db.Responses on q.Id equals r.QuestionId
               where q.PageNumber == page
               select new GetQuestionViewModel
                {
                   QuestionOptionRanking = qo.QuestionOptionRanking,
                   Id = q.Id,
                   Options = q.Options,
                   QuestionTypeId = q.QuestionTypeId,
                   Question1 = q.Question1,
                   QuestionRanking = q.QuestionRanking,
                   Answer = r.Answer
                 }).ToList();

           ViewBag.PageNumber = page;
           return View(question);
       }


My relationships is one Question to many QuestionOptions but as yet I have not found the correct syntax to return ANY of the QuestionOptions although all the other fields contain the data I expect. Can anyone see what I am doing wrong, please?
Posted
Updated 18-Jan-14 6:42am
v3
Comments
Karthik_Mahalingam 18-Jan-14 13:07pm    
what issue you are facing?
AlexHarmes 18-Jan-14 13:42pm    
When my View Returns, there are no values for join qo in db.QuestionOptions on q.Id equals qo.QuestionId but I know there is data in there. As I said it is a one to many relationship and it should be returning a list of QuestionOptions for reach question.
Karthik_Mahalingam 18-Jan-14 13:45pm    
i tried it is working fine..
AlexHarmes 18-Jan-14 14:05pm    
what is the query returning - and how can you try it without the DB behind?

1 solution

You can simulate with this

Questions List - main list
QuestionsOptions list - secondary list
Respose List - secondary list


for example: join 1 ( question list and question option list ) -> it will give 2 rows
join 1 results joined with Resonse list -> gives 4 rows as the parent(for second join) ( join 1 has 2 rows )



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using console_poc;

namespace oops1
{

    public class Db
    {
        public Db()
        {
            Questions = new List<Question>();
            Questions.Add(new Question() { Id = 1, Options = 1, PageNumber = 1, Question1 = "q1", QuestionRanking = 1, QuestionTypeId = 1 });


            QuestionOptions = new List<QuestionOption>();
            QuestionOptions.Add(new QuestionOption() { QuestionId = 1, QuestionOptionRanking = 1 });
            QuestionOptions.Add(new QuestionOption() { QuestionId = 1, QuestionOptionRanking = 2 });
            QuestionOptions.Add(new QuestionOption() { QuestionId = 2, QuestionOptionRanking = 3 });
            QuestionOptions.Add(new QuestionOption() { QuestionId = 2, QuestionOptionRanking = 4 });


            Responses = new List<Response>();
            Responses.Add(new Response() { Answer = "a1", QuestionId = 1 });

        }

        public List<QuestionOption> QuestionOptions { get; set; }
        public List<Question> Questions { get; set; }
        public List<Response> Responses { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {

            int page = 1;

            Db db = new Db();
           // using lamda expression
           //var result =  db.Questions.Join(db.QuestionOptions, (k => k.Id), (k => k.QuestionId), (q, qo) => new { q, qo }).
           //     Join(db.Responses, (k => k.q.Id), (k => k.QuestionId), (qqo, r) => new { r, qqo }).Where(k=>k.qqo.q.PageNumber == page).Select(k =>
           //         new GetQuestionViewModel()
           //         {
           //             QuestionOptionRanking = k.qqo.qo.QuestionOptionRanking,
           //             Id = k.qqo.q.Id,
           //             Options = k.qqo.q.Options,
           //             QuestionTypeId = k.qqo.q.QuestionTypeId,
           //             Question1 = k.qqo.q.Question1,
           //             QuestionRanking = k.qqo.q.QuestionRanking,
           //             Answer = k.r.Answer
           //         }).ToList();


            var question = (from q in db.Questions
                            join qo in db.QuestionOptions on q.Id equals qo.QuestionId
                            join r in db.Responses on q.Id equals r.QuestionId
                            where q.PageNumber == page
                            select new GetQuestionViewModel
                             {
                                 QuestionOptionRanking = qo.QuestionOptionRanking,
                                 Id = q.Id,
                                 Options = q.Options,
                                 QuestionTypeId = q.QuestionTypeId,
                                 Question1 = q.Question1,
                                 QuestionRanking = q.QuestionRanking,
                                 Answer = r.Answer
                             }).ToList();


        }
    }
}

public class Question
{
    public int Id { get; set; }
    public int PageNumber { get; set; }
    public Nullable<int> Options { get; set; }
    public string Question1 { get; set; }
    public int QuestionTypeId { get; set; }
    public Nullable<int> QuestionRanking { get; set; }
}


public class QuestionOption
{
    public int QuestionId { get; set; }
    public Nullable<int> QuestionOptionRanking { get; set; }
}
public class Response
{
    public int QuestionId { get; set; }
    public string Answer { get; set; }
}

public class GetQuestionViewModel
{
    public IEnumerable<Question> Questions { get; set; }
    public IEnumerable<QuestionOption> QuestionOptions { get; set; }
    public int Id { get; set; }
    public Nullable<int> Options { get; set; }
    public string QuestionOption1 { get; set; }
    public Nullable<int> QuestionOptionRanking { get; set; }
    public string Question1 { get; set; }
    public int QuestionTypeId { get; set; }
    public Nullable<int> QuestionRanking { get; set; }
    public string Answer { get; set; }
}
 
Share this answer
 
Comments
AlexHarmes 18-Jan-14 14:27pm    
Hmmm, this is still not working for me as it is only returning one value for QuestionOption. I seemed to have left it out of my select statement too, it should be QuestionOption1 = qo.QuestionOption1. It does not return a collection or list at all for me.

Also I want to make sure it only returns a single row for question then the list of options. I hope this makes sense to you as I really appreciate the help.

In my view it is breaking on this line;
@foreach (var item in q.QuestionOptions.OrderBy(o => o.QuestionOptionRanking))
Karthik_Mahalingam 18-Jan-14 15:01pm    
It returns a list<t> collection.
see if you have 1 id on left list and 2 on right list - > it will result in 2 row items
if 2 and 2 -> it wil reurn 4 row items.. since it is one to many..
AlexHarmes 18-Jan-14 15:37pm    
How can I write the query so it only returns one question and many options? I tried Groupby but that didn't work at all. Am I missing some syntax to do this correctly?
Karthik_Mahalingam 18-Jan-14 15:44pm    
it returns like that only..
question object properties will be duplicated and unique values of the QuestionOptions ..
AlexHarmes 18-Jan-14 16:09pm    
At first I was using this action;

public ActionResult ViewQuestion(int? id)
{
if (id == null || id == 0 || id > 13)
{
id = 8;
}
Question question = db.Questions.Find(id);
if (question == null)
{
return HttpNotFound();
}
return View(question);
}

It worked fine with the EDMX class Questions, but it was creating screens based on the Id field of Question and one question per screen. What I need is to sort on PageNumber as there can be more than one question on a page and UserId so I can get any answers already submitted by the logged in user. Should I add a screen shot to show you what I mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900