0

I am trying to join 3 tables using LINQ from 2 different SQL Servers (entities).

Error: The specified Linq expression contains references to queries that are associated with different contexts

var query = from a in EntityA.TableA
            join p in EntityA.TableB
            on a.PersonID equals p.PersonID
            join m in EntityB.TableC
            on Convert.ToInt32(a.SourceID) equals m.ID
            where p.someID == "100000527"
            select m.ID;

Please help me to resolve this.

Answer:

     var query = from a in EntityA.TableA
        join p in EntityA.TableB
        on a.PersonID equals p.PersonID
        where p.someID == "100000527"
        select a.ID;

    IQueryable<int> ID = null;

    foreach (var item in query)
    {
        int sourceID= Convert.ToInt32(item);
        ID = (from m in EntityB.TableC
              where m.ID == sourceID
              select m.ID).Distinct();
    }

    return ID;

Is this right approach?

Uba
  • 619
  • 3
  • 8
  • 20
  • I have seen that link. But I guess my scenario is little bit different because I am not getting System.NotSupportedException at all. I thought mine is same like http://stackoverflow.com/questions/7332920/the-specified-linq-expression-contains-references-to-queries-that-are-associated – Uba Apr 21 '14 at 22:28
  • Also Convert.ToInt32(a.SourceID) will not work anyway – AD.Net Apr 21 '14 at 22:50
  • Thanks. I modified it now. – Uba Apr 21 '14 at 22:54

1 Answers1

0

You can only write Linq to SQL Queries on one database at a time.

If you want to join the data together, you will have to write the two queries separately, create anonymous type objects from them, then join them together using plain old Linq on objects.

Cam Bruce
  • 5,632
  • 19
  • 34
  • I tried the following link, http://stackoverflow.com/questions/7332920/the-specified-linq-expression-contains-references-to-queries-that-are-associated. But, still no luck at all. :( – Uba Apr 21 '14 at 22:30