我有下面的代码,我试图通过我的qestAires匿名类型的孩子问题。 当我到达foreach循环时,我得到错误:
foreach语句不能对“问题”类型的变量进行操作,因为“问题”不包含“GetEnumerator”的公共定义
我需要做些什么来解决这个问题呢?
var questAires = (from qs in dc.Questionnaires from q in dc.Questions.Where(t => t.QuestionnaireId == qs.QuestionnaireID) from r in dc.Responses.Where(qr => qr.QuestionID == q.QuestionId).DefaultIfEmpty() where qs.QuestionnaireID == QuestionnaireId select new { qs.Description, Questions = q, Responses = r }).Single(); foreach(var question in questAires.Questions) { }I have the code below where I am trying to go through the child questions of my qestAires anonymous type. When I get to the foreach loop i get the error:
foreach statement cannot operate on variables of type 'Question' because 'Question' does not contain a public definition for 'GetEnumerator'
What do I need to do differently to get around this?
var questAires = (from qs in dc.Questionnaires from q in dc.Questions.Where(t => t.QuestionnaireId == qs.QuestionnaireID) from r in dc.Responses.Where(qr => qr.QuestionID == q.QuestionId).DefaultIfEmpty() where qs.QuestionnaireID == QuestionnaireId select new { qs.Description, Questions = q, Responses = r }).Single(); foreach(var question in questAires.Questions) { }最满意答案
questAires.Questions只会解决一个问题,你会为每个问题得到一个questAires对象(这将导致.Single()抛出)。
我想你想要这样的东西:
var questAires = (from qs in dc.Questionnaires select new { qs.Description, Questions = from q in dc.Questions where q.QuestionnaireId == qs.QuestionnaireID select new { Question = q, Response = (from r in dc.Responses where r.QuestionID == q.QuestionId select r).DefaultIfEmpty() } }).Single()questAires.Questions will only resolve to a single question, and you will get one questAires object for each question (which will cause .Single() to throw).
I guess you want something like this:
var questAires = (from qs in dc.Questionnaires select new { qs.Description, Questions = from q in dc.Questions where q.QuestionnaireId == qs.QuestionnaireID select new { Question = q, Response = (from r in dc.Responses where r.QuestionID == q.QuestionId select r).DefaultIfEmpty() } }).Single()更多推荐
发布评论