Linq with 2 different context using inner join

1

I would like to query 2 different databases (2 Dbcontext)

I have tried in several ways, one of them: I make a pre-query in the context DbMega to recover the officials

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

and then I query the other context with inner join with this pre query:

    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.Funtionary' can not be   constructed in a LINQ to Entities query.

If I change instead of Select new Funcionario leave 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 official ID and in the view calling a function that retrieves the user's name, but it was not so perfomous and I think this should be something normal an inner join on different banks, just do not know how to do it. p>     

asked by anonymous 17.03.2016 / 14:22

1 answer

1

Here is an example that I built 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 on the var t = linq.ToList(); line will confirm the completion of the existing record in both tables.

    
19.05.2016 / 20:52