开发者

Linq query is not populating Child Collection

The following query is not working from visual Studio:

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

however in linqPad it works like it should using:

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

The query is loading the questions that it should be loading, however it is not loading the child collection of AnswerKeys. However the linqpad query does return the child collection. After struggling for a day with this, I have to be doing something dumb...please tell me what my error is...thanks.

Adding more detail: I am using EF and RIA Services in a Silverlight 4.0 Application

I am executing this code from a partial Class of my DomainServiceClass

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;

        }

I dont know if that really makes any differenc开发者_StackOverflow中文版e. The linqpad query is using the same EF datacontext as this. I have verified that the generated SQL is the same. Would it matter that on the Client side its being returned as an 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()));
                }

            }

        }


It sounds to me like this.ObjectContext.Questions doesn't contain what you think it does. Have you tried running

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

...and then looking in the resulting list? That should give you an idea how to proceed. Mutatis mutandis regarding the .Include("SurveyQuestions") part, of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜