Answered by:
Tried to select question based on section id

Question
-
User1694748171 posted
updated
Tuesday, July 14, 2020 8:42 PM
Answers
-
User1694748171 posted
your code make infinite loop. I re-modified and its work
public List<Student_Exam_ViewModel> Piked_Questions() { Questions questions = new Questions(); Student_Exam_ViewModel gen = new Student_Exam_ViewModel (); var m1s = from A in db.Exam_Assessment // join q in db.Exam_Questions on A.Assessment_Section equals q.Assessment_Section select A ; var mod = new List<Student_Exam_ViewModel>(); foreach (var m1 in m1s) { var t = m1s.Count(); var q1s = db.Exam_Questions.Where(q => q.Assessment_Section == m1.Assessment_Section && m1.Amount_of_Test_Question > 0).Distinct(); ; var cqn = q1s.Select(x => x.QuestionName).Take(m1.Amount_of_Test_Question).ToList(); foreach (var q1 in q1s.Take(m1.Amount_of_Test_Question)) // itrate throught each column in db.Exam_Questions table { var model = new Student_Exam_ViewModel { GeneralQuestions = new List<Questions> { new Questions { QuestionId = q1.QuestionId, QuestionName = q1.QuestionName, Assessment_Section = q1.Assessment_Section, Amount_of_Question = m1.Amount_of_Test_Question, PossibleAnswers = new List<PossibleAnswer> { new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_1).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_2).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_3).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_4).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, } } } }; mod.Add(model); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 16, 2020 6:54 PM
All replies
-
User1120430333 posted
You should learn how to use {;} icon on the toolbar that formats your code before you post the code. The posted code is hard to read.
Wednesday, July 15, 2020 5:38 AM -
User-1330468790 posted
Hi luay20032003,
The problem is that the expression "A.Amount_of_Test_Question" will not be evaluated as an int in the Take() method. The Take() method only accept an "count(int)" as the parameter.
Therefore, I am afraid that you have to do a select first to fetch the value of "Amount_of_Test_Question" for the specific section and assign it into a int variable. Then, you could directly pass this variable to the Take() function.
You will still need to do an extra select even though you execute this query in plain SQL statement.
Best regardsd,
Sean
Wednesday, July 15, 2020 9:55 AM -
User303363814 posted
As the error message says, you will need a Constant Expression as the parameter to Take
var questionCount = db.Exam_Questions.Where(x => x.Assessmant_Section == A.Assessment_Section && A.Amount_Of_Test_Question > 0).First().Amount_of_Test_Question;
then you can Take the questions that you awant
Categories_QuestionName = db.Exam .... .Take(questionCount).ToList();
I don't understand the first sentence in your post but maybe I don't need to.
Are you sure you need to do this, it seems you have a rather odd database design? Without being able to see sample data it is difficult to understand. What happens if you leave Take out of the query?
Categories_QuestionName = db.Exam_Questiosn.Where(x => c.Assessment_Section == A.Assessmant_Section && A.Amount_of_Test_Question > 0).Select(x => x.QuestionName);
Wednesday, July 15, 2020 10:00 AM -
User1694748171 posted
If I leave Take() out of query this will bring all the questions that I don't need it. the logic is selection question base on assessment section and each assessment section there is amount of question need to show for example:
Assessment section 1.1 have only 2 question need to show
Assessment section 2.1 have only 5 question need to show
I can pass directly int to Take but that will be static and I want to fetch the amount of question directly from database to make it dynamically.
Wednesday, July 15, 2020 12:52 PM -
User1686398519 posted
Hi luay20032003,
You didn’t provider the fully model and dbcontext, I can’t figure out meaning of some variable. The relationship between the models cannot be guessed based on the attribute names, and even the role of many attributes is unknown.
public ActionResult Index(string StudentName) { var m1s = from A in db.Exam_Assessment join q in db.Exam_Questions on A.Assessment_Section equals q.Assessment_Section select A; var mod = new List<Questions>(); foreach (var m1 in m1s) { var q1s = db.Exam_Questions.Where(q => q.Assessment_Section == m1.Assessment_Section); var cqn = q1s.Select(x => x.QuestionName).Take(m1.Amount_of_Test_Question).ToList(); foreach(var q1 in q1s) { Questions questions = new Questions(); questions.QuestionName = q1.QuestionName; questions.Assessment_Section = q1.Assessment_Section; questions.Amount_of_Question = q1.Amount_of_Question; questions.QuestionId = q1.QuestionId; questions.Categories_QuestionName = cqn; questions.PossibleAnswers = new List<PossibleAnswer> { new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option1).FirstOrDefault(),QuestionId=q1.QuestionId}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option2).FirstOrDefault(),QuestionId=q1.QuestionId}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option3).FirstOrDefault(),QuestionId=q1.QuestionId}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option4).FirstOrDefault(),QuestionId=q1.QuestionId}, }; mod.Add(questions); } } var sevs = new List<Student_Exam_ViewModel> { new Student_Exam_ViewModel{ GeneralQuestions=mod } }; return View(sevs.ToArray()); }
Best Regards,
YihuiSun
Wednesday, July 15, 2020 1:07 PM -
User1694748171 posted
no need this question
Wednesday, July 15, 2020 1:45 PM -
User348806598 posted
Hi,
.Take(Amount_of_Question).FirstOrDefault()
Take with first or default does not make sense to me. If you need only one result, simply get first or default. If you need multiple result then get a list out of the result and remove first or default.
Wednesday, July 15, 2020 3:02 PM -
User1694748171 posted
no need
Wednesday, July 15, 2020 3:33 PM -
User1694748171 posted
I got infinite loop at the code
foreach (var q1 in q1s)
{
var r = q1s.Count();
Questions questions = new Questions();
questions.QuestionName = q1.QuestionName;
questions.Assessment_Section = q1.Assessment_Section;
//questions.Amount_of_Question = q1.Amount_of_Question;
questions.QuestionId = q1.QuestionId;
questions.Categories_QuestionName = cqn;
questions.PossibleAnswers = new List<PossibleAnswer>
{
new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_1).FirstOrDefault(),QuestionId=q1.QuestionId},
new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_2).FirstOrDefault(),QuestionId=q1.QuestionId},
new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_3).FirstOrDefault(),QuestionId=q1.QuestionId},
new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_4).FirstOrDefault(),QuestionId=q1.QuestionId},
};
mod.Add(questions);
}Thursday, July 16, 2020 1:48 AM -
User1694748171 posted
your code make infinite loop. I re-modified and its work
public List<Student_Exam_ViewModel> Piked_Questions() { Questions questions = new Questions(); Student_Exam_ViewModel gen = new Student_Exam_ViewModel (); var m1s = from A in db.Exam_Assessment // join q in db.Exam_Questions on A.Assessment_Section equals q.Assessment_Section select A ; var mod = new List<Student_Exam_ViewModel>(); foreach (var m1 in m1s) { var t = m1s.Count(); var q1s = db.Exam_Questions.Where(q => q.Assessment_Section == m1.Assessment_Section && m1.Amount_of_Test_Question > 0).Distinct(); ; var cqn = q1s.Select(x => x.QuestionName).Take(m1.Amount_of_Test_Question).ToList(); foreach (var q1 in q1s.Take(m1.Amount_of_Test_Question)) // itrate throught each column in db.Exam_Questions table { var model = new Student_Exam_ViewModel { GeneralQuestions = new List<Questions> { new Questions { QuestionId = q1.QuestionId, QuestionName = q1.QuestionName, Assessment_Section = q1.Assessment_Section, Amount_of_Question = m1.Amount_of_Test_Question, PossibleAnswers = new List<PossibleAnswer> { new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_1).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_2).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_3).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, new PossibleAnswer{Answer = db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.Answer_Option_4).FirstOrDefault(),QuestionId=q1.QuestionId ,AnswerId =db.Exam_Answers.Where(x=> x.QuestionId == q1.QuestionId).Select(x => x.AnswerId).FirstOrDefault()}, } } } }; mod.Add(model); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 16, 2020 6:54 PM