Lesson 11/31

Tutorials LINQ Mastery

Count, LongCount, Sum: Basic aggregations

On this page

Counting and Summing

Basic math in LINQ. These methods collapse a sequence of data into a single numeric value.

1. Count vs LongCount

Count() returns an int (max 2.1 billion). If you are processing truly 'Big Data' (like a logging table or financial transactions), use LongCount() which returns a long (64-bit integer).

2. The 'Sum' Method

Calculates the total of a numeric sequence. **Architect Note:** Sum will throw an exception if the sequence contains null values (for Nullable types), unless you handle them. For decimal?, Sum returns 0 if the list is empty, which is usually exactly what you want for financial reports.

3. Architect Insight

Q: "Is Count() on IQueryable fast?"

Architect Answer: "Yes. EF Core translates .Count() into a SELECT COUNT(*) SQL query. This is extremely fast because the database doesn't have to send any row data back—it just sends the single number. Always do your counting on the database sideWhenever possible."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

LINQ Mastery
Course syllabus
General
1. Core Foundations
2. Filtering & Transformation
3. Aggregation & Quantifiers
4. Ordering & Partitioning
5. Sets & Lookups
6. Join & Grouping
7. Advanced Providers & Parallelism
8. Real-world Performance & Patterns
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details