I made this linq in my 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
}).ToList().Distinct();
return _listaUnidade.ToList();
}
}
Trying to reproduce this selet that works the way I want:
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
Would you like to know what to do to work? In the select, with the Distinct, I only have 17 records, already in linq I bring more than 3 thousand. I just need to bring a UN for each reason, as it is in the query (select).
I did that and did not repeat the guys again, but the correct result is still not coming.
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(s => s.IDMotivo).DistinctBy(d => d.Codigo_Unidade_Negocio);
return _listaUnidade.ToList();
}
Then a colleague on another site told me and suggested this: You are using it in the wrong way, DistinctBy is used to get all the items that the property is 1 for example ...
then you are filtering all (DO NOT REPEAT) business unit code .DistinctBy (d => d Business Code)
and then you are filtering all (NO REPEAT) IDENTIFY DistinctBy (s => s.IDMotive); then there will only be one REASON ID of each ...
In your case, you need to compare if the object TAKING ACCOUNT OF 2 PROPERTIES implements Object.Equals () and Object.GetHashCode () in the TreeSource class
public override bool Equals(object obj)
{
if (!(obj is MontaArvoreAcao)) return false;
MontaArvoreAcao p = (MontaArvoreAcao)obj;
return Codigo_Unidade_Negocio == p.Codigo_Unidade_Negocio & IDMotivo == p.IDMotivo;
}
public override int GetHashCode()
{
return Codigo_Unidade_Negocio ^ IDMotivo;
}
And just use Distinct as it was before, now it will know how to compare if the objects are the same (Code Business Unit and IDMOTIVE).
But this line gives the error:
return Codigo_Unidade_Negocio ^ IDMotivo;
It says that the ^ operand can not be used in type string and int.
You are giving error in my GroupBy. Here's how I did it:
......).GroupBy(g => new { g.IDMotivo, g.Unidade_Negocio }).DistinctBy(d => d.Unidade_Negocio).DistinctBy(s => s.IDMotivo)
The error says that d.Unidade_Negocio
, which does not contain a definition for it.
I've come to the conclusion that this group will not do any good. I do not understand the following. I tried to make LINQ equal to QUERY that runs in the bank. Comparing everything, it seems that linq is a mirror of my query. The query gives me the correct result and linq does not. For example. I have 5 Reasons, with the following ID's. 1,2,3,4 and 5. In the reason of ID = 1, I have these UN's: DERMOCOSMETICS, GENERICS AND MIP. In Reason ID = 2, I have: DERMOCOSMETICS, GENERICS AND MIP. And so for ID = 3 and ID = 5. But in ID = 4, I have only: DERMOCOSMETICS and GENERICS. When I run LINQ and add it to my View, the following appears below each reason: MIP and GENERICS. This is for all reasons and this is not correct. I look at LINQ and I can not see anything. I do not know what could be wrong. This is the current model but what you suggested that I do:
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
{
Unidade_Negocio = a.Unidade_Negocio,
IDMotivo = r.IDMotivo
}
).DistinctBy(d => d.Unidade_Negocio).DistinctBy(s => s.IDMotivo).OrderBy(r => r.IDMotivo);
return _listaUnidade.ToList();
}
Dude, this is it. And I've been asked for more, and ... well, it's bone and I need to bone that bagasse. Any help, way, prayer is welcome.