0

I have the data below in a datatable this is example data. I would like get the occurrence of 12,13 in the datatable as normally there would be 10-20 million row in the datatable.

Customer  |  quantity  |  Product  | Code 

1         |  3         |  Product  | 12 
2         |  4         |  Product  | 13 
3         |  1         |  Product  | 12 
4         |  6         |  Product  | 13 
madth3
  • 7,275
  • 12
  • 50
  • 74
Jim Brad
  • 383
  • 1
  • 5
  • 12

2 Answers2

0

You can use Linq-To-DataTable:

int[] allowedCodes = new []{ 12, 13 };
var rows = table.AsEnumerable()
                .Where(r => allowedCodes.Contains(r.Field<int>("Code")));

However, if you have 10-20 million row in the datatable you should consider to do the filtering in the database itself.

If you want to know the number they occur:

int count = table.AsEnumerable()
                 .Count(r => allowedCodes.Contains(r.Field<int>("Code")));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

how about simple for each loop

private int getCount(int yourSearchDigit)
{
    int counter = 0;
    foreach (DataRow dr in youDataTable.Rows)
    {
        if (Convert.ToInt32(dr["Code"]) == yourSearchDigit)
        counter++;
    }
    return counter;
}
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44