locked
Possible breaking change beta2 -> RTM RRS feed

  • Question

  • Hi All,

    I've been upgrading my code from beta2 to RTM and my linq queries began to fail me.

    All of them are with join keyword.

    for example

     

    var result = from a in context.Table1

    join b in context.Table2 on a.Id equals b.Id

    where a.Name.Equals("MyName")

    select a;

     

    this query (over SSCE I might add) is throwing an exception :

    System.InvalidOperationException occurred
      Message="The query contains references to items defined on a different data context."
      Source="System.Data.Linq"
      StackTrace:
           at System.Data.Linq.SqlClient.QueryConverter.TranslateConstantTable(ITable table, SqlLink link)
           at System.Data.Linq.SqlClient.QueryConverter.CoerceToSequence(SqlNode node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitJoin(Expression outerSequence, Expression innerSequence, LambdaExpression outerKeySelector, LambdaExpression innerKeySelector, LambdaExpression resultSelector)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitAggregate(Expression sequence, LambdaExpression lambda, SqlNodeType aggType, Type returnType)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
           at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
           at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
           at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.ExecuteSleep(Expression expression)
           at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
           at Bar.foo(X x) in C:\myPath\myFile.cs:line 120
      InnerException:

     

    Well this query worked well under beta2, and I don't know what I do wrong, the syntax seems correct, ot I missed some breaking change in that matter.

    And Idea?

     

    Thanks

    Ariel

    Tuesday, December 4, 2007 8:24 AM

Answers

  • Ok guys,

    I believe I found the answer.

    The problem was that I was using a static DataContext.

    So when queries began running cocurrent that when the exception occures.

     

    So don't be lazy, create the Datacontext on every scope you have.

    (From the MSDN : "A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations").

     

    That was a *** Smile


    Ariel

     

    Wednesday, December 5, 2007 12:16 PM

All replies

  • Try:


    var result = from a in context.Table1

                     from b in context.Table2
                     where (a.Name.Equals("MyName") &&

                     (b.Id == a.Id))

                    select a;


    Bye.

    Tuesday, December 4, 2007 10:16 AM
  • Thanks, I tried that.

     

    I guess the problem is really in the result list itself.

    when I try something like result.Count<Table1Class>(), then the exception is thrown.

     

    System.InvalidOperationException was unhandled
      Message="The query contains references to items defined on a different data context."
      Source="System.Data.Linq"
      StackTrace:
           at System.Data.Linq.SqlClient.QueryConverter.CheckContext(SqlExpression expr)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMemberAccess(MemberExpression ma)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSelectMany(Expression sequence, LambdaExpression colSelector, LambdaExpression resultSelector)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequence(Expression exp)
           at System.Data.Linq.SqlClient.QueryConverter.VisitDefaultIfEmpty(Expression sequence)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.VisitAggregate(Expression sequence, LambdaExpression lambda, SqlNodeType aggType, Type returnType)
           at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
           at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
           at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
           at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
           at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
           at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.ExecuteSleep(Expression expression)
           at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
      InnerException:

    Still investigating.

    Any help is welcomed.

     

    Ariel

    Tuesday, December 4, 2007 10:32 AM
  • Ok guys,

    I believe I found the answer.

    The problem was that I was using a static DataContext.

    So when queries began running cocurrent that when the exception occures.

     

    So don't be lazy, create the Datacontext on every scope you have.

    (From the MSDN : "A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations").

     

    That was a *** Smile


    Ariel

     

    Wednesday, December 5, 2007 12:16 PM