Linq查询未填充Child Collection

时间:2011-03-01 18:22:36

标签: linq linq-to-entities linqpad

以下查询无法在visual Studio中使用:

 var query = this.ObjectContext.Questions.Include("AnswerKey").Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == 3));

然而在linqPad中它的工作原理应该是:

Questions
    .Include("AnswerKey")
    .Where(o=>o.SurveyQuestions.Any(o2=>o2.SurveyID==3)).Dump();

查询正在加载它应该加载的问题,但是它没有加载AnswerKeys的子集合。但是,linqpad查询确实返回子集合。经过这一天的挣扎,我必须做一些愚蠢的事情......请告诉我我的错误是什么......谢谢。

添加更多详细信息: 我在Silverlight 4.0应用程序中使用EF和RIA服务

我正在从我的DomainServiceClass的部分Class

执行此代码
public IQueryable<Data.Questions> GetQuestionsbySurveyId(int id)
        {
            //var query = this.ObjectContext.Questions.Include("AnswerKey").Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == id));
           // var query = this.ObjectContext.Questions.Include("AnswerKey").Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == 3));
           var query= this.ObjectContext.Questions.Include("AnswerKey").Include("SurveyQuestions");

            //var query = this.ObjectContext.Questions;

            Debug.WriteLine(((ObjectQuery)query).ToTraceString());

            return query;

        }

我不知道这是否真的有所不同。 linqpad查询使用与此相同的EF datacontext。我已经验证生成的SQL是相同的。在客户端,将其作为EntityQuery返回是否重要?

private void ReceiveNewQuestionairreRequest(fnReadmitPatientList_Result request)
        {
            CurrentSelectedPatient = request;

            var context = new SurveysDomainContext();
            EntityQuery<Questions> query = context.GetQuestionsbySurveyIdQuery(3);


            context.Load(query, SurveyQuestions_Loaded, null);
            //context.Load(q, AnswerKey_Loaded, null);

        }

        private void SurveyQuestions_Loaded(LoadOperation<Questions> lo)
        {
            if (!loHasError(lo))
            {

                QuestionsCollection = new ObservableCollection<Questions>(lo.Entities);
                foreach (Questions q in QuestionsCollection)
                {
                    Debug.WriteLine(String.Format("{0} {1} - Available Answers= {2}", q.ID, q.Text, q.AnswerKey.Count()));
                }

            }

        }

1 个答案:

答案 0 :(得分:0)

我觉得this.ObjectContext.Questions不包含您的想法。你试过跑吗

this.ObjectContext.Questions.Include("AnswerKey").Include("SurveyQuestions").ToList()

...然后查看结果列表?这应该会让你知道如何继续。 Mutatis mutandis 关于.Include("SurveyQuestions")部分,当然。