2

I have a datagrid that is filled with a dataset from an SQL query. What i would like to do is count how many occurrences there are of a certain entry, for example in Column 3 i would like to count how many times a string is repeated in the datagrid. Is there any way of doing this, perhaps with Linq? I initially tried doing this through an SQL query:

SELECT Count(*) Occurences
From myTable
WHERE Column_name Like 'stringPattern'

But the problem is that the database is pretty big and i already have the returned data so hoping to not have to re-query the database?

Any tips or suggestions would be appreciated.

Peter

4

2 回答 2

2

如果您已经有了要从数据集中查询的表,则可以使用 LINQ。

var count = myTable.AsEnumerable().Where(x => x["Column_name"] == "stringPattern").Count();
于 2012-07-03T13:55:09.713 回答
2

下面的工作是否...

原来的

SELECT Count(*) Occurences
From myTable
WHERE Column_name Like 'stringPattern'

数据表

var count = (from row in myTable.AsEnumerable()
             where string.Equals(row["Column_name"].ToString(), "stringPattern")
             select row).Count();
于 2012-07-03T13:55:15.080 回答