Bring another field in group by

Asked

Viewed 107 times

0

I have it

var grupo = from item in aliquotaProduto
group item by item.CFOP_ID into agrupamento
select new
{
    Categoria = agrupamento.Key,
    Quantidade = agrupamento.Count()
};

I can bring an ID_PROD in select in addition to Category and Quantity?

EDIT1

I did so

var grupo = from item in aliquotaProduto
    group item by item.CFOP_ID into agrupamento
    select new
    {
        Produto = agrupamento.Select(it => it.ID_PROD), 
        Categoria = agrupamento.Key,
        Quantidade = agrupamento.Count()
    };

but I’m having trouble working with it. I have the right Products, but I can’t do anything with them. How do I do that? How I now assign my share?

EDIT2

if I do so

var grupo = from item in objectProd
group item by new { item.CFOP_ID, item.ID_Prod };

I have it

Key = {{CFOP_ID = 5402, ID_PRO = 1 }} 
Key = {{CFOP_ID = 5404, ID_PRO = 2 }} 
Key = {{CFOP_ID = 5404, ID_PRO = 3 }}

if I do that:

foreach(var item in Lista)
{
    item.vlrUnit = rateio * varCount;

//a varCount deveria vir 1 para CFOP = 5402 e 2 para CFOP = 5404. É isso que eu não sei fazer.
}

that’s exactly what I need to do.

  • I doubt it’ll work, but you tried to add a .ToList() in the end? agrupamento.Select(it => it.ID_PROD).ToList()

  • The problem, @Leandroangelo, is exactly working and I don’t know how to do and I have to, you know,.

  • @Leandroangelo, so inside the product I have two items. A Prod with 5402 and quantity = 1 and the other with two Prod with 5404 and quantity = 2. I can’t get to item [1] with the two Prods. It’s hard to explain.

  • you asked that same question some other way before, see my answer? I didn’t want to go down that line?

  • @Leandroangelo, maybe I could not explain my doubt. The following: I have 3 items. These items I group by cfop. Soon this grouping gave two records. One item with 5402 and two with 5404. I need to take my share and multiply by these Counts(1 and 2) so that in thin I have: The record 1 multiplied by 1 and the other two records multiplied by 2. It turns out that my foreach has 3 items and if I do a foreach inside the other foreach with the items grouped, it will only cycle twice and the third item will not be multiplied.

  • You’ll still face the same problem... Foreach the grouping and multiply by the Product Count https://answall.com/questions/288656/agrupar-itens-dentro-de-um-foreach/288770#288770

Show 1 more comment

2 answers

2


try the following.:

var quantidades = (
    from item in objectProd 
    group item.CFOP_ID by item.CFOP_ID
).ToDictionary(grp => grp.Key, grp => grp.Count())

foreach(var produto in objectProd)
{
    var quantidade = quantidades[produto.CFOP_ID];
    produto.vlrUnit = rateio * quantidade;
}

1

I found a similar question in the SOEN and second she just put the fields directly in the group by:

var grupo = from item in aliquotaProduto
group item by new { item.CFOP_ID, item.CAMPO_CATEGORIA, item.CAMPO_QTD }
  • rLinhares, the way above I can not group, I have three lines, as they are 3 different products. What I want is if I have two equal CFOP, I add that and multiply by the apportionment. That’s what I’m not doing.

Browser other questions tagged

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