1
In the presentation layer I call a method consLaboratorio
who is in a namespace separate where all access to the data is carried out.
This method returns an object MiddleOneReturn
with: codeErro (0 ran without errors) and the result of the execution ("Object[]").
I recover the data through the following Cast: ((List<Laboratorio>)(md.Mensagem[0])).ToList();
, where Laboratorio
refers to the class mapped 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 into a namespace separated 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 method consLaboratorio
gets into a namespace with all other methods of data access 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 run a Join with return belonging to two distinct tables as 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 is not possible to perform the Cast.
The returned content is:
md.Mensagem[0]
Count = 1
[0]: { IdLaboratorio = 1, NmLaboratorio = "Laboratório 1", NmCidade = "Campinas" }
How to solve this problem?
You could create a "Lab" class with the fields
IdLaboratorio
,NmLaboratorio
andNmCidade
...– Andre Figueiredo
@ Andre Figueiredo It was the path I tried to follow "var local = ((List<Laboratoriocidade>)(Md.Message[0]). Tolist();" only returns error: Cannot convert an object of type 'System.Collections.Generic.List
1[<>f__AnonymousType2
3[System.Int64,System. String,System.String]' in type 'System.Collections.Generic.List`1[Estudodotnet.Laboratoriocidade]'.– Jothaz