Another DataContext error. Why is this a problem?

The following code causes the error "This request contains links to elements defined in a different data context." My 2 data contexts are created using 2 nested blocks using the code that calls this method and displays its results on the screen. The methods that this method invokes use only the data context it uses; they do not create any of their own. I confirmed that they are fine by inserting an additional return statement right before the one described below, and I have no problems, which makes me think that the problem is the LINQ instruction 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;
    }

, , IQueryable . , . , (, , ).

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

. .

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;
    }
+3
3

DataContex , .

LINQ to SQL , , , datacontexts, SQL? .

, , .

+2

:

from surveys in surveysInState
from cSurveyor in currentSurveyors

FINDataContext ? . , . , Log; SQL, , .

.

0

I fixed it. I still don't understand why I had a problem, but ok.

public static IQueryable<tblSurvey> GetSurveysInState(SurveyDataContext surveyContext,
        FINDataContext finContext, string state)
    {
        string[] facility = (from f in finContext.tblLocContacts
                       where f.State == state
                       select f.LocationID).ToArray();

        return from survey in surveyContext.tblSurveys
               where facility.Contains(survey.FacilityID)
               select survey;
    }
0
source

Source: https://habr.com/ru/post/1729457/


All Articles