Get higher separate value according to product code in datatable

Asked

Viewed 45 times

0

My project has a DataTable with four columns. I would like to know how to get only the items of this DataTable which, according to its occurrence, has the largest separate quantity?

For example:

Codigo|Descricao|Emgalagem|Separado
1010  |Celular  |12       |1
1010  |Celular  |12       |2
1010  |Celular  |12       |3
2020  |Tablet   |8        |1
2020  |Tablet   |8        |2
2020  |Tablet   |8        |3
2020  |Tablet   |8        |4
3030  |Laptop   |6        |1

In this case 1010 with three separate items and 2020 with four items and 3030 with only 1 item. How can I do this?

  • data comes from an SQL table?

  • 1

    No, they come as a form parameter

  • what would be the column that needs to get the highest value? A separate?

  • yes, it’s the separate...

1 answer

2


Try something like:

var result = from tab in dt.AsEnumerable()
          group tab by tab["Codigo"] into groupDt
             select new 
              { 
                Codigo = groupDt.Key,
                Max=groupDt.Max((r)=> int.Parse(r["Separado"].ToString()))
              };

    foreach(var item in result)
            Console.WriteLine(item.Codigo + ": " +item.Max);

put in fiddle for better viewing: https://dotnetfiddle.net/hq2LCz

  • I’ve updated the form, now it’s the way you need it

  • 1

    Good afternoon Leohenrique... your code worked! thank you so much for your help :)

Browser other questions tagged

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