In the presentation layer I call a consLaboratorio
method that is in a separate namespace where all data access is performed.
This method returns an object MiddleOneReturn
with: error_code (0-if run without errors) and execution result ("object []").
I retrieve the data through the following Cast : ((List<Laboratorio>)(md.Mensagem[0])).ToList();
, where Laboratorio
refers to the mapped class via entity framework.
EstudoDotNetNegocio objEstudoDotNet = new EstudoDotNetNegocio();
MiddleOneReturn md = new MiddleOneReturn();
md = objEstudoDotNet.consLaboratorio(1);
if (md.CodigoErro == 0)
{
var local = ((List<Laboratorio>)(md.Mensagem[0])).ToList();
string idLaboratorio = local.FirstOrDefault().IdLaboratorio.ToString();
string nomeLaboratorio = local.FirstOrDefault().NmLaboratorio.ToString();
}
MiddleOneReturn
gets in a separate namespace with other utility methods. It is used to traffic data between layers and has the following structure:
public class MiddleOneReturn
{
public MiddleOneReturn() { }
public MiddleOneReturn(int codigo, params object[] mensagem) {
CodigoErro = codigo;
Mensagem = mensagem;
}
public int CodigoErro { get; set; }
public object[] Mensagem { get; set; }
}
The consLaboratorio
method gets into a namespace with all other data access methods via Linq.
public MiddleOneReturn consLaboratorio(Int64 IdLaboratorio)
{
using (BDEntities db = new BDEntities())
{
var laboratorio = db.Laboratorio.Where(v => v.IdLaboratorio.Equals(IdLaboratorio)).ToList();
MiddleOneReturn md = new MiddleOneReturn();
md.CodigoErro = 0;
md.Mensagem = new object[] { laboratorio };
return md;
}
}
The problem is when I execute a join with a return that belongs to two distinct tables like the example below:
public MiddleOneReturn consLaboratorioCidade(Int64 IdLaboratorio)
{
using (BDEntities db = new BDEntities())
{
var laboratorio = (from l in db.Laboratorio
join c in db.Cidade on l.IdLaboratorio equals c.IdCidade into l_join_c
from c in l_join_c.DefaultIfEmpty()
where l.IdLaboratorio == IdLaboratorio
select new
{
l.IdLaboratorio,
l.NmLaboratorio,
c.NmCidade
}).ToList();
MiddleOneReturn md = new MiddleOneReturn();
md.CodigoErro = 0;
md.Mensagem = new object[] { laboratorio };
return md;
}
}
In the model, generated from the database, there is no class with the fields returned when executing the above command, so it can not cast .
The content returned is:
md.Mensagem[0]
Count = 1
[0]: { IdLaboratorio = 1, NmLaboratorio = "Laboratório 1", NmCidade = "Campinas" }
How can I solve this problem?