Linq with 2 different context using Inner Join

Asked

Viewed 1,377 times

1

I would like to make a query in 2 different databases (2 Dbcontext)

I tried it in several ways, one of them: I make a prequery in Dbmega context to recover the employees

    var FuncionariosLinq = (from Colaborador in dbMega.Funcionarios
                            select new Funcionario
                            {
                                FuncionarioId = Colaborador.FuncionarioId,
                                Nome = Colaborador.Nome,
                            }).ToArray();

and then do the consultation in the other context with inner join with this prior consultation:

    var Linq = (from sal in db.fechamentoSalarios
                join Funcionarios in FuncionariosLinq
               on sal.FuncionarioId equals Funcionarios.FuncionarioId into output
                where sal.Data.Year == DataFiltro.Year && sal.Data.Month == DataFiltro.Month
                select new GerenciarSalariosViewModel
                {
                    Data = sal.Data,
                    FuncionarioNome = output.FirstOrDefault().Nome
                    SalarioId = sal.SalarioId,
                    Valor = sal.Valor
                }
          );

Displays the error:

The Entity or Complex type 'WMB.Models.Funcionario' cannot be constructed in a LINQ to Entities query.

If I change instead of Select new Funcionario let select new { in:

var FuncionariosLinq = (from Colaborador in dbMega.Funcionarios
                                select new Funcionario
                                {
                                    FuncionarioId = Colaborador.FuncionarioId,
                                    Nome = Colaborador.Nome,
                                }).ToArray(); 

Displays the error:

Unable to create a Constant value of type 'Anonymous type'. Only Primitive types or enumeration types are supported in this context.

I decided to send the employee ID and in the view calling a function that retrieves the user name, but it was not so perfomatic and I believe that this should be something normal an Inner Join in different banks, I just do not know how to do.

1 answer

1


follows example that I set up based on your scenario and got the list correctly.

List<Funcionarios> Funcionarios = new List<Funcionarios>();
using (var dbMega = new Entities_Exemplo_A())
{
    Funcionarios.AddRange(dbMega.Funcionarios.ToList());
}

using (var db = new Entities_Exemplo_B())
{
    var linq = from Colaborador in Funcionarios
                join Sal in db.fechamentoSalarios on Colaborador.FuncionarioId equals Sal.FuncionarioId
                select new
                {
                    Data = Sal.Data,
                    FuncionarioNome = Colaborador.Nome,
                    SalarioId = Sal.SalarioId,
                    Valor = Sal.Valor
                };
    var t = linq.ToList();
}

Inserting a breakpoit in the line var t = linq.ToList();, will be confirmed the filling with the existing records in both tables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.