2013-01-07 43 views
0

我在数据表中有以下数据,这是示例数据。我想在数据表中得到12,13的出现,通常在数据表中会有10-20万行。检查数据表中出现的单词的出现

Customer | quantity | Product | Code 

1   | 3   | Product | 12 
2   | 4   | Product | 13 
3   | 1   | Product | 12 
4   | 6   | Product | 13 

回答

0

怎么样简单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; 
} 
0

您可以使用Linq-To-DataTable

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

但是,如果你在数据表10-20万行的,你应该考虑做过滤数据库本身。

如果你想知道电话号码,他们会出现:

int count = table.AsEnumerable() 
       .Count(r => allowedCodes.Contains(r.Field<int>("Code"))); 
+0

所以才会此计数已经发生的时间数量。 –

+0

@JimBrad如果你做了.Count()就可以了。 –

+0

@JimBrad:编辑我的答案,展示如何在没有选择任何东西的情况下对行进行计数。您也可以在Thomas提到的“Where”之后添加没有谓词的“Count”。 –

相关问题