我試圖從一個LINQ查詢檢索此:LINQ - 得到所有問題的答案都在一個結果
Question 1 - Answer 1
- Answer 2 (Selected)
- Answer 3
Question 2 - Answer 1
- Answer 2
- Answer 3 (Selected)
etc..
我的表是這樣的:
Question (with attached multilang support which I'll leave out for now)
QuestionAnswer
Answer (also with multilang)
Response (where the user's response is kept (aka which answer he took -> a specif QuestionAnswer row)
Questionnaire (where all the questionanswers for a single questionnaire are kept)
我已經嘗試以下,但我得到一個異常說,.ToList()不能被轉換成存儲過程時,我運行它(所以在執行時,而不是在編譯時)(注意這是翻譯):
(from culture in DbContext.Culture
from questionanswer in DbContext.QuestionAnswer
join questionnaire in DbContext.Questionnaire on questionanswer .QuestionnaireID equals questionnaire.QuestionnaireID
where culture.TwoLetterISO.Equals(cultureCode) &&
questionnaire.QuestionnaireID == id
select new QuestionnaireSectionInformation()
{
// Additional data is retrieved here, but thats not important for this question
Questions =
((from question in DbContext.Question
join qmultilang in DbContext.QuestionMultiLang on question.ID equals qMultiLang.Id
join response in DbContext.Response on questionanswer.ID equals response.questionanswerId into possibleReponse
where question.ID == questionanswer.QuestionID &&
qMultiLang.CultureId == culture.ID
select new Topic()
{
Question = qMultiLang.Vraag,
Opmerking = possibleResponse.Any() ? possibleResponse.FirstOrDefault().Commentaar : null,
Answers=
((from answer in DbContext.Answer
join aMultiLang in DbContext.AnswerMultiLang on answer.ID equals aMultiLang.Id
where aMultiLang.CultureId == culture.ID
select new Answer()
{
Answer= aMultiLang.Answer,
Selected = possibleAnswer.Any()
}).ToList())
}).ToList())
}).ToList();
我試圖學習一些更多的LINQ,這就是爲什麼我不把它拉開(通過檢索數據並提取出問題和答案)。
所以問題是:我得到一個異常說,當我運行它時,.ToList()不能被轉換爲存儲過程。如果我無法在它們上調用.ToList(),我如何獲得問題的所有子元素?
*我希望問題清楚* - 我不知道你的問題是什麼(也許這只是我)。另外,你發佈的數據庫模式不是一個模式,它只是一個表名列表。 – Groo
你正在做幾個'ToList()'的原因是什麼? – Arran
http://stackoverflow.com/questions/12515575/is-there-a-neat-way-of-doing-a-tolist-within-a-linq-query-using-query-syntax – m4ngl3r