Is there a method of merging equal items into a var list in LINQ in C#?

Asked

Viewed 62 times

5

Hello, good morning, good afternoon or good night, all right?

I have a method that obtains products from a database using LINQ in C#, but on that list there are products that are repeated, so you would need to make a union of these items and instead of having, for example, 4, to have only 1, because it would need to check whether product can be active or not, there is a method of making a union of these repeated items?

Follow the Code of the Consultation:

string TpDoc = "ORC";
string Ativo = "N";
var vendaPro = (from venPro in bdprincipalEntities.VendaProduto
                                join acab in bdprincipalEntities.Acabamento on venPro.CodAcabamento
                                 equals acab.CodAcabamento into vp_ac
                                from acab in vp_ac.DefaultIfEmpty()
                                join pro in bdprincipalEntities.produtos on venPro.Pro_codnosso
                                equals pro.Pro_codnosso into vp_p
                                from pro in vp_p.DefaultIfEmpty()
                                join precPro in bdprincipalEntities.Preco_Produto on venPro.Pro_codnosso
                                 equals precPro.Pre_Codnosso into pp_p
                                from precPro in pp_p.DefaultIfEmpty()
                                where precPro.Pre_Acabamento == venPro.CodAcabamento
                                orderby venPro.VenPro_Seq, venPro.VenPro_SeqItem
                                select new
                                {
                                    Ven_CodigoPre = venPro.Ven_CodigoPre,
                                    Pro_codnosso = venPro.Pro_codnosso,
                                    Pro_descricao = pro.Pro_descricao,
                                    CodAcabamento = venPro.CodAcabamento,
                                    DescAcabamento = acab.DescAcabamento,
                                    Pre_Ativo = precPro.Pre_Ativo,
                                    Pro_ativo = pro.Pro_ativo,
                                }).Where(v => v.Ven_CodigoPre == Ven_codigoPre).Where(v => v.Pro_ativo == Ativo || v.Pre_Ativo == Ativo)
                                .ToList();

As a final result of this method, I obtain the following information:

{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
}

Where the result of the union would be something like this:

{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
}

NOTE: The list with the results is in json, but, it is only to illustrate the final result of the query.

I’ve seen some bonding methods, but most of them require you to have 2 lists, in this case, it’s just a list with repeated products, besides, in this example, it’s being listed only 1 product repeated several times, then, the method I would need would be one that binds not only a product with several repetitions, an example would be that result below:

{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "ABA02",
    "Pro_descricao": "COMPONENTE ADICIONAL",
    "CodAcabamento": "BR/BR",
    "DescAcabamento": "BRANCO/BRANCO",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "ABA02",
    "Pro_descricao": "COMPONENTE ADICIONAL",
    "CodAcabamento": "BR/BR",
    "DescAcabamento": "BRANCO/BRANCO",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "ABA02",
    "Pro_descricao": "COMPONENTE ADICIONAL",
    "CodAcabamento": "BR/BR",
    "DescAcabamento": "BRANCO/BRANCO",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
}

Where the end result would look like this:

{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "LD83/Q/MINIDIC",
    "Pro_descricao": "LD 83 ORIENT. 1MINIGZ10 (BD/FD)",
    "CodAcabamento": "BT/BT",
    "DescAcabamento": "BRANCO TEXTURIZADO/BRANCO TEXTURIZ.",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
},
{
    "Ven_CodigoPre": 82664,
    "Pro_codnosso": "ABA02",
    "Pro_descricao": "COMPONENTE ADICIONAL",
    "CodAcabamento": "BR/BR",
    "DescAcabamento": "BRANCO/BRANCO",
    "Pre_Ativo": "N",
    "Pro_ativo": "S"
}

Thanks in advance for your collaboration and help.

  • Use group by

  • I tried using the Groupby method before the conversion of . Tolist(), the code does not return errors, however, the records are not grouped, follows the model:(...).Where(v => v.Pro_ativo == Ativo || v.Pre_Ativo == Ativo).GroupBy(p => p.Pro_codnosso).ToList() . Is there any other method I can use or any additional conditions I need to put in the current code?

  • Of a read on that here that will definitely help you.

2 answers

2

You can use the method Distinct();, for equal items in all attributes (this may not work well for complex objects, so you would have to use the same function, but overwriting the comparator).

Alternatively, you could use groupBy together with select: suaList.GroupBy(x => new {x.Propriedade1, x.Propriedade2}).Select(y => y.First()).ToList(); to compare only some attributes and return a list without repetitions.

1

Before the .Tolist();, add .Distinct(), as an example below:

List<int> semNumerosRepetidos = numeros.Distinct().ToList();

Browser other questions tagged

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