Distinct in the Plains does not work

Asked

Viewed 1,101 times

3

I did this Illumination on 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 play this selet that works as 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 make it work? In select, with Distinct, I bring only 17 records, already in Brasilia 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 so and did not repeat the guys, but still does not come the correct result.

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 the wrong way, Distinctby serves to get all the items that that property is 1 for example...

then vc is filtering all (WITHOUT REPEATING) business unit code . Distinctby(d => d.Code)

and then you are filtering all (WITHOUT REPEATING) IDMOTIVO Distinctby(s => s.Idmotivo); so there will only be one MOTIF ID of each...

In your case how you need to compare if the object TAKING INTO ACCOUNT THE 2 PROPERTIES, implements Object.Equals() and Object.Gethashcode() in the Montaarvoreace 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 equal (Code Business Unit and IDMOTIVO).

But on that line there’s a mistake:

return Codigo_Unidade_Negocio ^ IDMotivo;

It says that the operand cannot be used in string and int type.

You are making a mistake in my Groupby. See how I did:

......).GroupBy(g => new { g.IDMotivo, g.Unidade_Negocio }).DistinctBy(d => d.Unidade_Negocio).DistinctBy(s => s.IDMotivo)

The mistake says d.Unidade_Negocio, which does not contain a definition for it.

I have come to the conclusion that this group will be of no use. I do not understand the following. I tried to make the LINQ equal to the QUERY that runs in the bank. Comparing everything, it seems that Linus is a mirror of my query. The query brings me the correct result and Linus does not. For example. I have 5 Motifs, with the following ID’s. 1,2,3,4 and 5. In the Motif of ID=1, I have these UN’s: DERMOCOSMETICS, GENERIC AND MIP. In Motif ID=2, I have: DERMOCOSMETICS, GENERIC 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 mount in my View, appears below each motif the following: MIP and GENERICS. That’s for all reasons and that’s not right. I look at LINQ and I can’t see anything. I don’t know what could be wrong. This is the current model but what you suggested 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, it’s all right here. And I’ve been asked for more stuff, and... well, it’s bone-bone and I need to bone this. Any help, walk, prayer is welcome.

  • 2

    pnet, you tried to give the distinct before running the first . Tolist() ?

2 answers

4

That’s not how you use the Distinct(). The Distinct() filters only identical objects, which is not the case with your query.

Use the method DistinctBy of the Nuget Morelinq package:

https://www.nuget.org/packages/morelinq/1.1.0

Use:

return _listaUnidade.DistinctBy(l => l.IDMotivo).ToList();

3

Says the documentation of the Distinct:

Returns the distinct elements of a sequence using the standard equality comparator to compare values.

Emphasis on comparador de igualdade padrão.

The standard equality comparison for anonymous types is the reference comparison. As each of the more than three thousand elements has a different address in memory, the comparison between any two elements will always indicate that they are different. Even if the data is the same.

To solve this, create your own type of data to store this information - and overload the method Equals to compare data instead of references.

Or you can do as Vinicius suggested and execute the method distinct before transforming database data into list elements ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.