Reputation: 85036
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)
{
}
Upvotes: 1
Views: 982
Reputation: 34391
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()
Upvotes: 5
Reputation: 11759
q actually resolves to a single item from the enumerable dc.Questions.Where(...)
, so yeah, you're only going to get a single item - not an enumerable - for Questions.
Upvotes: 0