1
That is the mistake:
An exception of type 'System.InvalidCastException' occurred in App_Web_fjskumyk.dll but was not handled in user code
Additional information: Não é possível converter um objeto do tipo 'System.Collections.Generic.List`1[<>f__AnonymousType5`2[System.String,System.String]]' no tipo 'System.Collections.Generic.List`1[Ruptura.Models.MontaArvoreAcao]'.
I have tried everything and still not walking much in my code. See what I did in my Controller that returns the data:
var _listaUnidade = (
from r in db.Ruptura
join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
where r.IDMotivo != 6
select new
{
a.Codigo_Unidade_Negocio,
a.Unidade_Negocio
}).ToList().Distinct();
ViewData["ListaUn"] = _listaUnidade.ToList();
Now at the moment of filling the page, is giving the stick. See my foreach in View, which is with the problems already cited.
......
<ul>
@foreach (var un in (List<Ruptura.Models.MontaArvoreAcao>) ViewData["ListaUn"])
{
<li item-checked='false' item-expanded='false'>
@un.Unidade_Negocio
</li>
}
</ul>
......
So it gave no more error, but distinct it did not work.
var _listaUnidade = (
from r in db.Ruptura
join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
where r.IDMotivo != 6
select new MontaArvoreAcao
{
Codigo_Unidade_Negocio = a.Codigo_Unidade_Negocio,
Unidade_Negocio = a.Unidade_Negocio
}).ToList().Distinct();
ViewData["ListaUn"] = _listaUnidade.ToList();
I tried to make a similar one to that select(That works distinct)
select distinct a.Unidade_Negocio, r.IDMotivo
from Ruptura r
join Apresentacao a on a.Codigo_Apresentacao = r.Codigo_Apresentacao
join Motivo m on r.IDMotivo = m.IDMotivo
group by r.IDMotivo,a.Unidade_Negocio
order by r.IDMotivo
The way below I’m practically us finally, just a problem I don’t know where it comes from. For example, I have 5 Reasons listed. This is correct. For Idmotivo(1,2 and 3) I have 3 UN(Dermocosmetics, MIP and Generics). He is only listing me 2 UN for each Reason. The ID = 4 motif, I have only two UN(Generic and MIP) and Motif ID = 5 I have 3 UN(Generic, MIP and Dermocosmetics). It turns out that only 2 UN(Generic and MIP) is coming for everyone. Is there anything else missing? Here’s my code.
Model:
public static List<MontaArvoreAcao> CriarListaArvoreAcao()
{
RupturaEntities db = new RupturaEntities();
var _listaUnidade = (
from r in db.Ruptura
join a in db.Apresentacao on r.Codigo_Apresentacao equals (a.Codigo_Apresentacao)
join m in db.Motivo on r.IDMotivo equals (m.IDMotivo)
where r.IDMotivo != 6
select new MontaArvoreAcao
{
Codigo_Unidade_Negocio = a.Codigo_Unidade_Negocio,
Unidade_Negocio = a.Unidade_Negocio,
IDMotivo = r.IDMotivo
}
).ToList().Distinct().DistinctBy(d => d.Codigo_Unidade_Negocio).DistinctBy(s => s.IDMotivo);
return _listaUnidade.ToList();
}
My Controller:
public ActionResult Acao()
{
ViewData["ListaUn"] = MontaArvoreAcao.CriarListaArvoreAcao();
return View(MontaArvoreAcao.montaArvoreAcao());
}
My View:
<ul>
@foreach (var item in Model)
{
if (_motivo != @item.Motivo)
{
_idmotivo = @item.IDMotivo;
<li item-checked='false' item-expanded='false'>
@item.Motivo
<ul>
@foreach (var un in (List<Ruptura.Models.MontaArvoreAcao>) ViewData["ListaUn"])
{
<li item-checked='false' item-expanded='false'>
@un.Unidade_Negocio
</li>
}
</ul>
</li>
}
_motivo = @item.Motivo;
}
</ul>
It seems your problem is in
select new
, because you’re only going for two fields, and in the view you’re saying that viewData is a certain type of data (Rupture.Models.Montaarvoreacao)– CesarMiguel
If I declare my _list();
– pnet
If you delete select and distinct shows results? Correct repeat?
– CesarMiguel
As I gave a new Montaarvoreacao, I had to say that a certain field of this class is equivalent to a certain field of Linus. When this was done, the error disappeared, but the performance fell to unbearable levels. Since I started writing this commentary, I haven’t opened the page.
– pnet
The distinct didn’t work. I tried to make it so it didn’t repeat the same UN and repeated, so it was slow.
– pnet
Yes, it was just to try. But it presented data, correct?
– CesarMiguel
I made an issue, to show how it is and what I want.
– pnet
@pnet As you are updating the question, it turns out that the answer may be outdated as well. The initial problem was the error, now it seems that you have a problem in the query. I recommend to update the question with the new question, open another question and keep your original question.
– Renan
Okay, it is that we are finding solutions throughout the help of colleagues, that we do not even notice and end up changing the course of the original post. But I already asked another question yes.
– pnet