Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ and the Count extension method

Tags:

c#

linq-to-sql

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?

like image 397
Martin Avatar asked Mar 09 '09 14:03

Martin


People also ask

Does LINQ use extension methods?

LINQ provides many extension methods for filtering, grouping, sorting and many more which will make developers' lives easy.

What is count method in C#?

The Count() method is an extension method of IEnumerable included in System. Linq. Enumerable class. It can be used with any collection or a custom class that implements IEnumerable interface. All the built-in collections in C#, such as array, ArrayList, List, Dictionary, SortedList, etc.

What are methods in LINQ?

In LINQ, Method Syntax is used to call the extension methods of the Enumerable or Queryable static classes. It is also known as Method Extension Syntax or Fluent. However, the compiler always converts the query syntax in method syntax at compile time.


1 Answers

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

like image 112
Chris Shaffer Avatar answered Sep 22 '22 12:09

Chris Shaffer