Linq分组.include(“Table”)在Table上返回null

时间:2009-12-14 11:20:49

标签: c# linq group-by

我有一个linq查询,按QuestionGroup的答案分组。

我需要加载表AssessmentQuestionsReference,以便我可以在我的WPF应用程序中绑定它。

   var groupedAnswers = from a in App.ents.AssessmentAnswers.Include("AssessmentQuestions")
                        where a.Organisations.OrganisationID == App.selectedOrganisation.OrganisationID
                        group a by a.AssessmentQuestions.AssessmentQuestionGroups.QuestionGroup into g
                        select new { Group = g.Key, Answer = g };

当我向下钻取g时,AssessmentQuestions为“null”。我不知道为什么因为我认为它应该加载它甚至没有包含,因为我正在通过该表来获取问题组。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您是否尝试过加入AssessmentQuestions.AssessmentQuestionGroups

您的.Include("AssessmentQuestions")将会a.AssessmentQuestions,而不是a.AssessmentQuestions.AssessmentQuestionGroups

答案 1 :(得分:0)

我添加了类似检查字符串并包括与组工作意外。这很奇怪,但工作

var yy = (from r in context.RateSet.Include(x => x.Currency).Include(y => y.Currency1)
                      select r).ToList();
var xx = (from r in context.RateSet.Include(x => x.Currency).Include(y => y.Currency1)
                  orderby r.DateRate, r.Currency.NameCurrency
                  group r by new { r.IdFromCurrency, r.IdToCurrency} into gp
                  select gp.FirstOrDefault()).ToList();