Pergunta

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 
Foi útil?

Solução

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;
}

Outras dicas

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")));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top