6

I have a database table which could contain many records and I'd like to count the current total in the table. I was going to do a simple:

DataContext.Table.Count(c => c.condition);

Until I realized the return type for Count is int. What if the table is to hold more values than can be represented in 32 bits? How can I count them?

Should I be counting them in a different way when we're talking about that kind of scale?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Martin
  • 425
  • 1
  • 6
  • 11
  • Even though LongCount() extension method is your solution but in all likely hood you will get out of memory exception at run time if your filter result set is exceeding the number represented by `int.MaxValue`. In fact it will fail way below `int.MaxValue`, forget going to `long` (BIGINT in SQL) range of records. CLR restricts the max allowed size of an object to 2 GB. Please consider re-architecting your application if you really happen to bring those many records in memory. More details here - http://stackoverflow.com/questions/1087982/single-objects-still-limited-to-2-gb-in-size-in-clr-4-0 – RBT Aug 19 '16 at 06:18

2 Answers2

10

Use LongCount(), same thing but with a 64 bit result.

Chris Shaffer
  • 32,199
  • 5
  • 49
  • 61
0

My solution was to use the .LongCount() extension method.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Martin
  • 425
  • 1
  • 6
  • 11
  • Don't use answers to reply to other posters. Use comments or edit your question. This is not a discussion forum. – GEOCHET Mar 09 '09 at 15:06
  • It sounded to me like he found the answer himself and posted it, rather than replying. – Mark Pattison Mar 09 '09 at 15:11
  • @Mark: His original post was an apology for not finding it. I have edited it. – GEOCHET Mar 09 '09 at 15:12
  • I wasn't replying to other posters btw, I was replying to my own question - but yes I did also apologise for not looking harder before I left the original question. It made less sense to edit my question with an answer than to answer it. – Martin Mar 09 '09 at 15:40