SQL count(*) performance

Mikael Eriksson has a good explanation bellow why the first query is fast: SQL server optimize it into: if exists(select * from BookChapters). So it goes looking for the presence of one row instead of counting all the rows in the table. For the other two queries, SQL Server would use the following rule. To … Read more

Getting a count of rows in a datatable that meet certain criteria

One easy way to accomplish this is combining what was posted in the original post into a single statement: int numberOfRecords = dtFoo.Select(“IsActive=”Y””).Length; Another way to accomplish this is using Linq methods: int numberOfRecords = dtFoo.AsEnumerable().Where(x => x[“IsActive”].ToString() == “Y”).ToList().Count; Note this requires including System.Linq.