Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 582933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:44:21+00:00 2026-05-13T14:44:21+00:00

The following code is causing a This query contains references to items defined on

  • 0

The following code is causing a “This query contains references to items defined on a different data context” error. My 2 data contexts are created with 2 nested using blocks around the code that calls this method and displays its results on screen. The methods that this method calls only use the data context passed in to them, they don’t create any of their own. I have verified that they are ok by inserting a extra return statement right before the one in the method below and I don’t get any problems, which leads me to believe the problem is in the LINQ statement on the return line… What am I doing wrong?

public static IQueryable<tblSurveyor> GetPossibleSurveyorsForSurvey(SurveyDataContext surveyContext,
        FINDataContext finContext, int surveyID)
    {
        IQueryable<tblSurveyor> currentSurveyors = 
            GetSurveyorsForSurvey(surveyContext, surveyID);

        tblSurvey currentSurvey = GetSurvey(surveyContext, surveyID);

        tblLocContact facility = GetFacility(finContext, currentSurvey.FacilityID);

        IQueryable<tblSurvey> surveysInState = GetSurveysInState(surveyContext, finContext,
            facility.State);

        return from task in surveyContext.tblSurveyor_Tasks
               from surveys in surveysInState
               from cSurveyor in currentSurveyors
               from surveyors in surveyContext.tblSurveyors
               where surveyors.SurveyorID != cSurveyor.SurveyorID &&
               surveys.SurveyID == task.SurveyID &&
               task.SurveyorID == surveyors.SurveyorID
               select surveyors;
    }

I’ve changed a few things, and most notable I got rid of the IQueryable variables and made them arrays. This was primairly to force enumeration as I went. This revealed that the problem (or at least one problem in in this method).

tblSurvey[] surveysInState = GetSurveysInState(surveyContext, finContext,
            state).ToArray();

Here is that method’s implementation. I still don’t see the problem with it.

public static IQueryable<tblSurvey> GetSurveysInState(SurveyDataContext surveyContext,
        FINDataContext finContext, string state)
    {
        return from survey in surveyContext.tblSurveys
               from facility in finContext.tblLocContacts
               where survey.FacilityID == facility.LocationID && facility.State == state
               select survey;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:44:21+00:00Added an answer on May 13, 2026 at 2:44 pm

    You are mixing various DataContex in same query, that’s the problem.

    LINQ to SQL defer query execution until the data is enumerated, so if you mix datacontexts how would it resolve the query with a SQL statement? It can’t resolve it.

    A possible solution is to have all related data in the same datacontext or you could enumerate the data and mix it later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Facebook doesn't allow conventional JavaScript in canvas applications. If you're… May 15, 2026 at 11:52 am
  • Editorial Team
    Editorial Team added an answer There is an example of this on this Swing examples… May 15, 2026 at 11:52 am
  • Editorial Team
    Editorial Team added an answer You can dynamically load jQuery only if it is needed.… May 15, 2026 at 11:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.