(Linq / Lambda) Join 2 or more tables with 2 DBContext

Im getting an error like this: The LINQ expression specified contains query references related to different contexts.

var employee = new ApplicationDbContext().Employee;
var otherTable = new OtherDbContext().OtherTable;

var returnValue = (from e in employee
                        join o in otherTable on e.Id equals o.Id
                        select new
                        {
                            e.Name,
                            e.Address,
                            o.Others
                        });

      

Any solution / s? Thank!

+3


source to share


2 answers


You should instantiate your DBC context in general and not specify the table / model. Example:

 private ApplicationDBContext db = new ApplicationDbContext();

      



Then choose whether you want to use LINQ or Raw SQL. I believe you are more familiar with SQL because you mentioned joining tables, why not use that? Here is a tutorial on using Raw SQL.

If you still insist on using LINQ and include join, here's a good link for it.

+2


source


List<Employee> empList = new ApplicationDbContext().Employee.ToList();
List<OtherTable> othrList= new OtherDbContext().OtherTable.ToList();

var returnValue = (from e in empList 
                        join o in othrList on e.Id equals o.Id
                        select new
                        {
                            e.Name,
                            e.Address,
                            o.Others
                        });

      



try it, it will work .....

0


source







All Articles