Marcelo Dias
Marcelo Dias

Reputation: 429

Why I'm still getting System.NotSupportedException

My code is:

using( var ctxA = new AEntities())
using( var ctxB = new BEntities())
{
    var listOfA = (from A in AEntities.AEntity select A).ToList();
    var listOfB = (from B in BEntities.BEntity
                   where listOfA.Select( A = A.Id)contains(B.Id) 
                   select B).ToList();
}

I'm getting the error:

 The specified LINQ expression contains references to queries that are associated with different contexts.

However, because of 'ToList' I already did a fetch in AEntity thats makes second query only about one of the contexts, did not?

How can a separate the two queries still using one list to query the other?

Upvotes: 0

Views: 108

Answers (2)

Marcelo Dias
Marcelo Dias

Reputation: 429

If anyone wants to know, the answer to my question was:

var listOfA = (from A in AEntities.AEntity select A.Id).ToList();
List<long> listOfIdsOfA = listOfA.Select( A=>A.id).ToList();
var listOfB = (from B in BEntities.BEntity
           where listOfIdsOfA.Contains(B.Id) 
           select B).ToList();

The only difference to Selman's answer was the creation of Id's list after the first query. That's why I need the whole entity too.

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

Try to store just IDs into listOfA

 var listOfA = (from A in AEntities.AEntity select A.Id).ToList();
 var listOfB = (from B in BEntities.BEntity
               where listOfA.Contains(B.Id) 
               select B).ToList();

Upvotes: 1

Related Questions