-2

my problem is occurred when I tried to grouping data from two tables in this controller as below:

public class QuestionModelsController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    public ActionResult Quiz()
    {
        var Ans = from ansewrs in db.AnswerModels
                 join question in db.QuestionModels
                 on  ansewrs.QuestionID equals question.ID

                 select ansewrs;
        var grouped = from j in Ans
                      group j by j.Question.QuestionText
                      into gr
                      select new CheckListModel
                      {
                          QuestionText = gr.Key,
                          Questions = gr

                      };
        return View(grouped.ToList());
    }

My ViewModels are:

public class CheckListModel
{
    public string QuestionText { get; set; }
    public virtual IEnumerable<QuestionModel> Questions { get; set;  }

}

public class QuestionModel
{
    public int ID { set; get; }
    public string QuestionText { set; get; }
    public string SelectedAnswer { set; get; }
    public virtual IList<AnswerModel> Answers { set; get; }


}
public class AnswerModel
{
    public int ID { set; get; }
    public string AnswerText { set; get; }

    public int QuestionID { get; set; }
    public virtual QuestionModel Question { get; set; }

}

This row Questions = gr is not recognized , thus this error will occur: Error CS0266 Cannot implicitly convert type 'System.Linq.IGrouping<string, MyAssembly.AnswerModel>' to 'System.Collections.Generic.IEnumerable<MyAssembly.QuestionModel>'. An explicit conversion exists (are you missing a cast?)

Please help me and Thank you very much

4
  • You can try to use Questions = gr.ToList(), but you have a different type of source and destination collections, AnswerModel and QuestionModel. It makes sense to review the logic of your queries Mar 28, 2020 at 15:45
  • Try Questions = gr.Select(a => a.Question)
    – devNull
    Mar 28, 2020 at 15:49
  • As an aside, it looks as though your CheckListModel is just a grouping of QuestionModels by QuestionText. You could probably simplify the whole query by completely removing the join with the AnswerModels
    – devNull
    Mar 28, 2020 at 16:03
  • Thanks very much Pavel Anikhouski and devNull for your help , I found the error is here: just I replace "public virtual IEnumerable<QuestionModel> Questions { get; set; }" by public virtual IEnumerable<AnswerModel>Questions { get; set; } Mar 28, 2020 at 18:16

1 Answer 1

1

"Questions" is typed as being a sequence of questions. You are giving it a sequence of groups of answers. The problem here isn't with the code; rather, the compiler is doing its job to explain that there is something conceptually wrong with what you are doing. You've made some error where you've confused "sequence of questions" with "groups of answers"; resolve that discrepancy and rewrite the code appropriately.

2
  • Yes sir , my request "Questions" is typed as being a sequence of questions. and I'm giving it a sequence of groups of answers. How can I resolve that discrepancy and rewrite the code appropriately. Please help me sir and thank you very much Mar 28, 2020 at 17:07
  • Thank you sir Eric Lippert very much for your help , error was here: just I replace "public virtual IEnumerable<QuestionModel> Questions { get; set; }" by public virtual IEnumerable<AnswerModel>Questions { get; set; } , the problem solved Mar 28, 2020 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.